ActiveComposedConstructionSet.java from Texai at Krugle
Show ActiveComposedConstructionSet.java syntax highlighted
/*
* ActiveComposedConstructionSet.java
*
* Created on January 25, 2007, 3:00 PM
*
* Description: Provides an indexed container for the set of active composed constructions.
*
* Copyright (C) 2007 Stephen L. Reed.
*
* This program is free software; you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program;
* if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package org.texai.grammar.understanding;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
*
* @author reed
*/
public final class ActiveComposedConstructionSet {
/** the set of active composed constructions */
private Set<ActiveComposedConstruction> activeComposedConstructions =
new HashSet<ActiveComposedConstruction>();
/** the construction name dictionary, name -> set of active constructions having name */
private Map<String, Set<ActiveComposedConstruction>> constructionNameDictionary =
new HashMap<String, Set<ActiveComposedConstruction>>();
/**
* Creates a new instance of ActiveComposedConstructionSet.
*/
public ActiveComposedConstructionSet() {
super();
}
/** Adds the given active composed construction to the set.
*
* @param activeComposedConstruction the given active composed construction
*/
public void add(final ActiveComposedConstruction activeComposedConstruction) {
//Preconditions
assert activeComposedConstruction != null : "activeComposedConstruction must not be null";
activeComposedConstructions.add(activeComposedConstruction);
final String name = activeComposedConstruction.getName();
Set<ActiveComposedConstruction> activeComposedConstructionsHavingName = constructionNameDictionary.get(name);
if (activeComposedConstructionsHavingName == null) {
activeComposedConstructionsHavingName = new HashSet<ActiveComposedConstruction>();
constructionNameDictionary.put(name, activeComposedConstructionsHavingName);
}
activeComposedConstructionsHavingName.add(activeComposedConstruction);
}
/** Returns true if the set contains the given construction.
*
* @param activeComposedConstruction the given active composed construction
* @return true if the set contains the given construction
*/
public boolean contains(final ActiveComposedConstruction activeComposedConstruction) {
//Preconditions
assert activeComposedConstruction != null : "activeComposedConstruction must not be null";
return activeComposedConstructions.contains(activeComposedConstruction);
}
/** Returns true if the set contains one or more constructions with the given name.
*
* @param name the given name
* @return true if the set contains one or more constructions with the given name
*/
public boolean containsKey(final String name) {
//Preconditions
assert name != null : "name must not be null";
return constructionNameDictionary.containsKey(name);
}
/** Returns the set of constructions with the given name.
*
* @param name the given name
* @return the set of constructions with the given name
*/
public Set<ActiveComposedConstruction> get(final String name) {
//Preconditions
assert name != null : "name must not be null";
return constructionNameDictionary.get(name);
}
/** Removes the given active composed construction from the set.
*
* @param activeComposedConstruction the given active composed construction
*/
public void remove(final ActiveComposedConstruction activeComposedConstruction) {
//Preconditions
assert activeComposedConstruction != null : "activeComposedConstruction must not be null";
activeComposedConstructions.remove(activeComposedConstruction);
final String name = activeComposedConstruction.getName();
final Set<ActiveComposedConstruction> activeComposedConstructionsHavingName = constructionNameDictionary.get(name);
assert activeComposedConstructionsHavingName != null : "expected set not found for " + name;
activeComposedConstructionsHavingName.remove(activeComposedConstruction);
}
/** Returns an iterator for read-only purposes.
*
* @return an iterator for read-only purposes over the set of active composed constructions
*/
public Iterator<ActiveComposedConstruction> iterator() {
return activeComposedConstructions.iterator();
}
/** Returns a string representation of this object.
*
* @return a string representation of this object
*/
@Override
public String toString() {
return activeComposedConstructions.toString();
}
}
See more files for this project here