001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (C) Alkacon Software (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.util;
029
030import java.util.Collections;
031import java.util.HashMap;
032import java.util.Map;
033
034import com.google.gwt.user.client.rpc.IsSerializable;
035
036/**
037 * A utility class used for keeping track of a set of objects. You can explicitly include or exclude objects,
038 * and define a default membership value for those objects whose membership hasn't been explicitly set.<p>
039 *
040 * @param <T> the element type
041 */
042public class CmsDefaultSet<T> implements IsSerializable {
043
044    /** The default membership value for objects. */
045    private boolean m_defaultMembership;
046
047    /** The map for keeping track of the explicitly set memberships. */
048    private HashMap<T, Boolean> m_membershipMap = new HashMap<T, Boolean>();
049
050    /** Flag which controls whether this object can be modified. */
051    private boolean m_frozen;
052
053    /**
054     * Checks that this object isn't frozen.<p>
055     */
056    public void checkNotFrozen() {
057
058        if (m_frozen) {
059            throw new IllegalStateException("Can't modify frozen default set.");
060        }
061    }
062
063    /**
064     * Returns true if the given object is a member of this set.<p>
065     *
066     * @param value the value to check
067     *
068     * @return true  if the value is a member
069     */
070    public boolean contains(T value) {
071
072        Boolean isMember = m_membershipMap.get(value);
073        if (isMember != null) {
074            return isMember.booleanValue();
075        } else {
076            return m_defaultMembership;
077        }
078    }
079
080    /**
081     * Makes the object unmodifiable.<p>
082     */
083    public void freeze() {
084
085        m_frozen = true;
086    }
087
088    /**
089     * Gets the map internally used for storing the membership statuses.<p>
090     *
091     * @return the membership map
092     */
093    public Map<T, Boolean> getBaseMap() {
094
095        return Collections.unmodifiableMap(m_membershipMap);
096    }
097
098    /**
099     * Gets the default membership value.<p>
100     *
101     * @return the default membership value
102     */
103    public boolean getDefaultMembership() {
104
105        return m_defaultMembership;
106    }
107
108    /***
109     * Sets the membership of an object.<p>
110     *
111     * @param value the object
112     * @param isMember true if the object should be a member, otherwise false
113     */
114    public void setContains(T value, boolean isMember) {
115
116        checkNotFrozen();
117        m_membershipMap.put(value, Boolean.valueOf(isMember));
118    }
119
120    /**
121     * Sets the default membership value.<p>
122     *
123     * @param defaultMembership the new value
124     */
125    public void setDefaultMembership(boolean defaultMembership) {
126
127        checkNotFrozen();
128        m_defaultMembership = defaultMembership;
129    }
130}