krysalis-update/src/java/org/krysalis/depot/common/util/collection EntryCollection.java,NONE,1.1 EntityList.java,NONE,1.1

Nick Chalko <[email protected]> Wed, 08 Dec 2004 09:06:41 +0000
Newsgroups gmane.comp.krysalis.cvs
Message-ID <[email protected]>
Update of /cvsroot/krysalis/krysalis-update/src/java/org/krysalis/depot/common/util/collection
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24726/src/java/org/krysalis/depot/common/util/collection

Added Files:
	EntryCollection.java EntityList.java 
Log Message:
Copied from the apache incubator.

--- NEW FILE: EntryCollection.java ---
/*
 * Copyright  2004 The Apache Software Foundation
 *
 *  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 org.krysalis.depot.common.util.collection;

import java.util.Collections;

/**
 * @author ajack
 */

//:TODO: TreeSet again, to compare/avoid duplicates...
public class EntryCollection extends EntityList {

	public EntryCollection(String name) {
		super(name);
	}

	public void sort() {
		Collections.sort(this);
	}
}

--- NEW FILE: EntityList.java ---
/*
 * Copyright  2004 The Apache Software Foundation
 *
 *  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 org.krysalis.depot.common.util.collection;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import org.krysalis.depot.common.util.debug.DebugUtils;
import org.krysalis.depot.common.util.debug.Dumpable;
import org.krysalis.depot.common.util.dom.DOMProducer;
import org.krysalis.depot.common.util.dom.DOMUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * @version $Revision: 1.1 $
 */
public class EntityList extends ArrayList implements Dumpable, Comparable, DOMProducer {

	private String m_name = null;

	/**
	 * Limiting to a max size (FIFO = First In, First Out)
	 */
	private int m_maxSize = 0;

	/**
	 * Constructor for ConstraintSet.
	 * @param initialCapacity
	 */
	public EntityList(String name, int initialCapacity) {
		super(initialCapacity);
		m_name = name;
		init();
	}

	/**
	 * Constructor for ConstraintSet.
	 */
	public EntityList(String name) {
		super();
		m_name = name;
		init();
	}

	/**
	 * Constructor for ConstraintSet.
	 * @param c
	 */
	public EntityList(String name, Collection c) {
		super(c);
		m_name = name;
		init();
	}

	private void init() {
		if (null == m_name)
			m_name = "EntitySet";
	}

	public boolean hasEntries() {
		return !isEmpty();
	}

	public void dump(PrintWriter out, int depth, boolean verbose) {
		String indent = DebugUtils.getIndent(depth);
		String indent1 = DebugUtils.getIndent(depth + 1);

		out.print(indent);
		out.print("Name : ");
		out.println(m_name);
		if (hasEntries()) {
			out.print(indent);
			out.println("(");
			for (Iterator i = iterator(); i.hasNext();) {
				Object entry = (Object) i.next();

				if (entry instanceof Dumpable) {
					((Dumpable) entry).dump(out, depth + 1, verbose);
					out.println();
				}
				else {
					out.print(indent1);
					out.println(entry.toString());
				}
			}
			out.print(indent);
			out.println(")");
		}
		else {
			out.print(indent);
			out.println("No Entries.");
		}
	}
	/**
	 * Returns the name.
	 * @return String
	 */
	public String getName() {
		return m_name;
	}

	/**
	 * Sets the name.
	 * @param name The name to set
	 */
	public void setName(String name) {
		m_name = name;
	}

	private boolean hasLimit() {
		return m_maxSize != 0;
	}
	private void capLimit() {
		if (m_maxSize != 0)
			return;
		while (size() > m_maxSize) {
			remove(0);
		}
	}

	/* (non-Javadoc)
	 * @see java.util.List#add(int, java.lang.Object)
	 */
	public void add(int arg0, Object arg1) {
		super.add(arg0, arg1);
		if (hasLimit())
			capLimit();
	}

	/* (non-Javadoc)
	 * @see java.util.Collection#add(java.lang.Object)
	 */
	public boolean add(Object arg0) {
		boolean added = super.add(arg0);

		if (hasLimit())
			capLimit();

		return added;
	}

	/* (non-Javadoc)
	 * @see java.util.Collection#addAll(java.util.Collection)
	 */
	public boolean addAll(Collection arg0) {
		boolean added = super.addAll(arg0);

		if (hasLimit())
			capLimit();

		return added;
	}

	/* (non-Javadoc)
	 * @see java.util.List#addAll(int, java.util.Collection)
	 */
	public boolean addAll(int arg0, Collection arg1) {
		boolean added = super.addAll(arg0, arg1);

		if (hasLimit())
			capLimit();

		return added;
	}

	/**
	 * @return
	 */
	public int getMaxSize() {
		return m_maxSize;
	}

	/**
	 * @param i
	 */
	public void setMaxSize(int i) {
		m_maxSize = i;
	}

	public int compareTo(Object other) {
		return m_name.compareTo(((EntityList) other).m_name);
	}
	
	public void produceDOM(Document document, Element element) {
		DOMUtils.produceDOM(document, element, m_name, (Collection)this);
	}
}



-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now. 
http://productguide.itmanagersjournal.com/