com.phloc.commons.collections.attrs.MapBasedReadonlyAttributeContainer Maven / Gradle / Ivy
/**
* Copyright (C) 2006-2015 phloc systems
* http://www.phloc.com
* office[at]phloc[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.phloc.commons.collections.attrs;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.NotThreadSafe;
import com.phloc.commons.ValueEnforcer;
import com.phloc.commons.annotations.ReturnsMutableCopy;
import com.phloc.commons.collections.ContainerHelper;
import com.phloc.commons.hash.HashCodeGenerator;
import com.phloc.commons.string.ToStringGenerator;
/**
* Default implementation of the {@link IReadonlyAttributeContainer} based on a
* hash map. This implementation may carry null
values but that is
* not recommended.
*
* @author Philip Helger
*/
@NotThreadSafe
public final class MapBasedReadonlyAttributeContainer extends AbstractReadonlyAttributeContainer
{
private final Map m_aAttrs;
public MapBasedReadonlyAttributeContainer (@Nonnull final Map aMap)
{
ValueEnforcer.notNull (aMap, "Map");
m_aAttrs = ContainerHelper.newMap (aMap);
}
public MapBasedReadonlyAttributeContainer (@Nonnull final IReadonlyAttributeContainer aCont)
{
ValueEnforcer.notNull (aCont, "Container");
// Must already be a copy!
m_aAttrs = aCont.getAllAttributes ();
}
public boolean containsAttribute (@Nullable final String sName)
{
return m_aAttrs.containsKey (sName);
}
@Nonnull
@ReturnsMutableCopy
public Map getAllAttributes ()
{
return ContainerHelper.newMap (m_aAttrs);
}
@Nonnull
public Enumeration getAttributeNames ()
{
return ContainerHelper.getEnumeration (m_aAttrs.keySet ());
}
@Nonnull
@ReturnsMutableCopy
public Set getAllAttributeNames ()
{
return ContainerHelper.newSet (m_aAttrs.keySet ());
}
@Nonnull
@ReturnsMutableCopy
public Collection