Here you can find the source of getTimeBucket(TimeUnit unit, long timestamp, int bucketSizeInSeconds)
Parameter | Description |
---|---|
timeInMilliSeconds | a parameter |
bucketSizeInSeconds | a parameter |
public static int getTimeBucket(TimeUnit unit, long timestamp, int bucketSizeInSeconds) throws IllegalArgumentException
//package com.java2s; /**/*from w w w.j a v a 2s. co m*/ * Copyright 2016 Ambud Sharma * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.util.concurrent.TimeUnit; public class Main { private static final IllegalArgumentException ARGUMENT_EXCEPTION = new IllegalArgumentException(); /** * Floor long time to supplied Window * * @param timeInMilliSeconds * @param bucketSizeInSeconds * @return windowedTime */ public static int getTimeBucket(TimeUnit unit, long timestamp, int bucketSizeInSeconds) throws IllegalArgumentException { int ts = timeToSeconds(unit, timestamp); return getWindowFlooredNaturalTime(ts, bucketSizeInSeconds); } /** * Convert supplied timestamp to seconds; * * @param time * @param unit * @return */ public static int timeToSeconds(TimeUnit unit, long time) { int ts; switch (unit) { case NANOSECONDS: ts = (int) (time / (1000 * 1000 * 1000)); break; case MICROSECONDS: ts = (int) (time / (1000 * 1000)); break; case MILLISECONDS: ts = (int) (time / 1000); break; case SECONDS: ts = (int) time; break; default: throw ARGUMENT_EXCEPTION; } return ts; } /** * Floor integer time to supplied Window * * @param timeInSeconds * @param bucketInSeconds * @return windowedTime */ public static int getWindowFlooredNaturalTime(int timeInSeconds, int bucketInSeconds) { /** * bit shifting division and multiplication doesn't outperform the JIT compiler */ // return (timeInSeconds >> bucketInSeconds) << bucketInSeconds; return ((timeInSeconds / bucketInSeconds) * bucketInSeconds); } }