Java TimeUnit Calculate getTimeBucket(TimeUnit unit, long timestamp, int bucketSizeInSeconds)

Here you can find the source of getTimeBucket(TimeUnit unit, long timestamp, int bucketSizeInSeconds)

Description

Floor long time to supplied Window

License

Apache License

Parameter

Parameter Description
timeInMilliSeconds a parameter
bucketSizeInSeconds a parameter

Return

windowedTime

Declaration

public static int getTimeBucket(TimeUnit unit, long timestamp, int bucketSizeInSeconds)
        throws IllegalArgumentException 

Method Source Code

//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);
    }
}

Related

  1. getMilliseconds(int interval, TimeUnit unit)
  2. getNowTimeUnit(TimeUnit timeUnit)
  3. getParentUnit(TimeUnit unit)
  4. getProperUnitName(TimeUnit unit, long amount)
  5. getRandomTimeRound(int _duration, TimeUnit _unit)
  6. getTimeInMillis(TimeUnit unit)
  7. getTimeString(long value, TimeUnit unit)
  8. getTimeUnit(Object units)
  9. getTimeUnit(String timeUnit, TimeUnit defaultUnit)