jp.co.ntt.atrs.domain.common.masterdata.PeakTimeProvider.java Source code

Java tutorial

Introduction

Here is the source code for jp.co.ntt.atrs.domain.common.masterdata.PeakTimeProvider.java

Source

/*
 * Copyright 2014-2018 NTT Corporation.
 *
 * 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 jp.co.ntt.atrs.domain.common.masterdata;

import jp.co.ntt.atrs.domain.model.PeakTime;
import jp.co.ntt.atrs.domain.repository.peaktime.PeakTimeRepository;

import org.joda.time.DateTime;
import org.joda.time.Interval;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

/**
 * ???
 * 
 * @author NTT 
 */
@Component
public class PeakTimeProvider {

    /**
     * ?
     */
    @Inject
    PeakTimeRepository peakTimeRepository;

    /**
     * ?
     */
    private final List<PeakTime> peakTimeList = new ArrayList<>();

    /**
     * ???
     */
    @PostConstruct
    public void load() {
        peakTimeList.addAll(peakTimeRepository.findAll());
    }

    /**
     * ?????
     * 
     * @param depDate ?
     * @return ??????null
     */
    public PeakTime getPeakTime(Date depDate) {
        Assert.notNull(depDate);

        for (PeakTime peakTime : peakTimeList) {
            Interval peakTimeInterval = new Interval(
                    new DateTime(peakTime.getPeakStartDate()).withTimeAtStartOfDay(),
                    new DateTime(peakTime.getPeakEndDate()).withTimeAtStartOfDay().plus(1));
            // ?????
            if (peakTimeInterval.contains(depDate.getTime())) {
                return peakTime;
            }
        }
        return null;
    }

}