All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.objectos.way.base.util.Holidays Maven / Gradle / Ivy

There is a newer version: 2.9.0
Show newest version
/*
 * Copyright 2014 Objectos, Fábrica de Software LTDA.
 *
 * 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.
 */
package br.com.objectos.way.base.util;

import java.util.Set;

import org.joda.time.Days;
import org.joda.time.LocalDate;

import br.com.objectos.way.core.lang.HasLocalDate;

import com.google.common.base.Function;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Iterables;
import com.google.common.primitives.Ints;

/**
 * @author [email protected] (Marcio Endo)
 */
class Holidays {

  private static final LocalDate ZERO = new LocalDate(1970, 1, 1);

  private Holidays() {
  }

  public static int[] toIntArray(Set holidays) {
    Iterable integers = Iterables.transform(holidays, ToInt.INSTANCE);
    Set ints = ImmutableSortedSet.copyOf(integers);
    return Ints.toArray(ints);
  }

  public static int intValue(HasLocalDate holiday) {
    LocalDate date = holiday.getDate();
    return intValue(date);
  }

  public static int intValue(LocalDate date) {
    return Days.daysBetween(ZERO, date).getDays();
  }

  private enum ToInt implements Function {
    INSTANCE;
    @Override
    public Integer apply(HasLocalDate input) {
      return Holidays.intValue(input);
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy