001/*
002 * Copyright (c) 2007-2015 Concurrent, Inc. All Rights Reserved.
003 *
004 * Project and contact information: http://www.cascading.org/
005 *
006 * This file is part of the Cascading project.
007 *
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *     http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020
021package cascading.util;
022
023import java.util.ArrayList;
024import java.util.Collections;
025import java.util.Comparator;
026import java.util.List;
027import java.util.Map;
028import java.util.TreeMap;
029
030/**
031 *
032 */
033public class SortedListMultiMap<K, V> extends MultiMap<List<V>, K, V>
034  {
035  Comparator<K> comparator = null;
036  int initialListSize = 10;
037
038  public SortedListMultiMap()
039    {
040    }
041
042  public SortedListMultiMap( int initialListSize )
043    {
044    this.initialListSize = initialListSize;
045    }
046
047  public SortedListMultiMap( Comparator<K> comparator, int initialListSize )
048    {
049    this.comparator = comparator;
050    this.initialListSize = initialListSize;
051    }
052
053  public V get( K key, int pos )
054    {
055    List<V> multiValues = getMultiValues( key );
056
057    return multiValues.get( pos );
058    }
059
060  public V set( K key, int pos, V value )
061    {
062    List<V> multiValues = getMultiValues( key );
063
064    return multiValues.set( pos, value );
065    }
066
067  @Override
068  protected Map<K, List<V>> createMap()
069    {
070    return new TreeMap<>( comparator );
071    }
072
073  @Override
074  protected List<V> createCollection()
075    {
076    return new ArrayList<>( initialListSize );
077    }
078
079  @Override
080  protected List<V> emptyCollection()
081    {
082    return Collections.emptyList();
083    }
084
085  public Map.Entry<K, List<V>> firstEntry()
086    {
087    return ( (TreeMap) getMap() ).firstEntry();
088    }
089
090  public Map.Entry<K, List<V>> pollFirstEntry()
091    {
092    return ( (TreeMap) getMap() ).pollFirstEntry();
093    }
094  }