au.id.hazelwood.xmltvguidebuilder.model.ChannelListings.java Source code

Java tutorial

Introduction

Here is the source code for au.id.hazelwood.xmltvguidebuilder.model.ChannelListings.java

Source

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 au.id.hazelwood.xmltvguidebuilder.model;

import org.apache.commons.collections15.Factory;
import org.apache.commons.collections15.map.LazyMap;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;

/**
 * @author Ricky Hazelwood
 * @version 1.0
 */
public class ChannelListings {
    private final Map<Integer, ChannelDetail> channelById;
    private final Map<Integer, SortedSet<ProgrammeDetail>> programsByChannelId;

    public ChannelListings() {
        channelById = new LinkedHashMap<>();
        programsByChannelId = LazyMap.decorate(new HashMap<>(), new ProgrammeDetailSortedSetFactory());
    }

    public List<ChannelDetail> getChannels() {
        return Collections.unmodifiableList(new ArrayList<>(channelById.values()));
    }

    public SortedSet<ProgrammeDetail> getPrograms(Integer channelId) {
        return Collections.unmodifiableSortedSet(programsByChannelId.get(channelId));
    }

    public void addChannel(ChannelDetail channelDetail) {
        channelById.put(channelDetail.getId(), channelDetail);
    }

    public void addProgram(Integer channelId, ProgrammeDetail programmeDetail) {
        programsByChannelId.get(channelId).add(programmeDetail);
    }

    public int getNumberOfPrograms(Integer channelId) {
        return programsByChannelId.get(channelId).size();
    }

    private static class ProgrammeDetailComparator implements Comparator<ProgrammeDetail> {
        @Override
        public int compare(ProgrammeDetail o1, ProgrammeDetail o2) {
            int result = o1.getStartDate().compareTo(o2.getStartDate());
            if (result == 0) {
                result = o1.getDuration().compareTo(o2.getDuration());
            }
            return result;
        }
    }

    private static class ProgrammeDetailSortedSetFactory implements Factory<SortedSet<ProgrammeDetail>> {
        @Override
        public SortedSet<ProgrammeDetail> create() {
            return new TreeSet<>(new ProgrammeDetailComparator());
        }
    }
}