001/*
002 * JPPF.
003 * Copyright (C) 2005-2018 JPPF Team.
004 * http://www.jppf.org
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.jppf.utils.collections;
020
021import java.util.*;
022
023/**
024 * A sorted map whose values are sets of a given component type.
025 * @param <K> the type of keys in the map.
026 * @param <V> the type of values in the collections mapped to the keys.
027 * @author Laurent Cohen
028 */
029public abstract class AbstractCollectionSortedMap<K, V> extends AbstractCollectionMap<K, V> implements CollectionSortedMap<K, V> {
030  /**
031   * Explicit serialVersionUID.
032   */
033  private static final long serialVersionUID = 1L;
034  /**
035   * Comparator used to sort the keys.
036   */
037  protected final Comparator<K> comparator;
038
039  /**
040   * Default cosntructor.
041   */
042  public AbstractCollectionSortedMap() {
043    this(null);
044  }
045
046  /**
047   * Initialize this collection sorted map with the specified comparator.
048   * @param comparator comparator used to sort the keys.
049   */
050  public AbstractCollectionSortedMap(final Comparator<K> comparator) {
051    this.comparator = comparator;
052    map = createMap();
053  }
054
055  @Override
056  protected Map<K, Collection<V>> createMap() {
057    return comparator == null ? new TreeMap<K, Collection<V>>() : new TreeMap<K, Collection<V>>(comparator);
058  }
059
060  @Override
061  @SuppressWarnings("unchecked")
062  public K firstKey() {
063    return ((SortedMap<K, V>) map).firstKey();
064  }
065
066  @Override
067  @SuppressWarnings("unchecked")
068  public K lastKey() {
069    return ((SortedMap<K, V>) map).lastKey();
070  }
071}