com.helger.commons.collection.multimap.AbstractMultiTreeMapMapBased Maven / Gradle / Ivy
/**
* Copyright (C) 2014-2015 Philip Helger (www.helger.com)
* philip[at]helger[dot]com
*
* 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 com.helger.commons.collection.multimap;
import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.NotThreadSafe;
import com.helger.commons.state.EChange;
/**
* Abstract multi map based on {@link java.util.TreeMap}.
*
* @author Philip Helger
* @param
* outer key type
* @param
* inner key type
* @param
* value type
*/
@NotThreadSafe
public abstract class AbstractMultiTreeMapMapBased extends TreeMap > implements IMultiMapMapBased
{
public AbstractMultiTreeMapMapBased ()
{}
public AbstractMultiTreeMapMapBased (@Nullable final Comparator super KEYTYPE1> aComparator)
{
super (aComparator);
}
public AbstractMultiTreeMapMapBased (@Nullable final KEYTYPE1 aKey,
@Nullable final KEYTYPE2 aInnerKey,
@Nullable final VALUETYPE aValue)
{
putSingle (aKey, aInnerKey, aValue);
}
public AbstractMultiTreeMapMapBased (@Nullable final KEYTYPE1 aKey, @Nullable final Map aValue)
{
put (aKey, aValue);
}
public AbstractMultiTreeMapMapBased (@Nullable final Map extends KEYTYPE1, ? extends Map > aCont)
{
if (aCont != null)
putAll (aCont);
}
@Nonnull
protected abstract Map createNewInnerMap ();
@Nonnull
public Map getOrCreate (@Nullable final KEYTYPE1 aKey)
{
Map aCont = get (aKey);
if (aCont == null)
{
aCont = createNewInnerMap ();
super.put (aKey, aCont);
}
return aCont;
}
@Nonnull
public final EChange putSingle (@Nullable final KEYTYPE1 aKey,
@Nullable final KEYTYPE2 aInnerKey,
@Nullable final VALUETYPE aValue)
{
return EChange.valueOf (getOrCreate (aKey).put (aInnerKey, aValue) != null);
}
@Nonnull
public final EChange putAllIn (@Nonnull final Map extends KEYTYPE1, ? extends Map > aMap)
{
EChange eChange = EChange.UNCHANGED;
for (final Map.Entry extends KEYTYPE1, ? extends Map > aEntry : aMap.entrySet ())
for (final Map.Entry aEntry2 : aEntry.getValue ().entrySet ())
eChange = eChange.or (putSingle (aEntry.getKey (), aEntry2.getKey (), aEntry2.getValue ()));
return eChange;
}
@Nonnull
public final EChange removeSingle (@Nullable final KEYTYPE1 aKey, @Nullable final KEYTYPE2 aInnerKey)
{
final Map aCont = get (aKey);
return aCont == null ? EChange.UNCHANGED : EChange.valueOf (aCont.remove (aInnerKey) != null);
}
@Nullable
public final VALUETYPE getSingle (@Nonnull final KEYTYPE1 aKey, @Nonnull final KEYTYPE2 aInnerKey)
{
final Map aCont = get (aKey);
return aCont == null ? null : aCont.get (aInnerKey);
}
public final boolean containsSingle (@Nullable final KEYTYPE1 aKey, @Nullable final KEYTYPE2 aInnerKey)
{
final Map aCont = get (aKey);
return aCont != null && aCont.containsKey (aInnerKey);
}
@Nonnegative
public final long getTotalValueCount ()
{
return MultiMapMapBasedHelper.getTotalValueCount (this);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy