001: /*
002: ItsNat Java Web Application Framework
003: Copyright (C) 2007 Innowhere Software Services S.L., Spanish Company
004: Author: Jose Maria Arranz Santamaria
005:
006: This program is free software: you can redistribute it and/or modify
007: it under the terms of the GNU Affero General Public License as published by
008: the Free Software Foundation, either version 3 of the License, or
009: (at your option) any later version. See the GNU Affero General Public
010: License for more details. See the copy of the GNU Affero General Public License
011: included in this program. If not, see <http://www.gnu.org/licenses/>.
012: */
013:
014: package org.itsnat.impl.core.domutil;
015:
016: import java.util.Collection;
017: import java.util.Iterator;
018: import java.util.List;
019: import java.util.ListIterator;
020: import org.itsnat.core.ItsNatException;
021: import org.itsnat.core.domutil.ElementList;
022: import org.itsnat.core.domutil.ElementListFree;
023: import org.itsnat.impl.core.util.*;
024: import org.w3c.dom.Element;
025:
026: /**
027: *
028: * @author jmarranz
029: */
030: public class ElementListFreeUtil {
031:
032: public static int indexOf(ElementListFreeInternal list, Object o) {
033: return list.indexOfElement((Element) o);
034: }
035:
036: public static boolean contains(ElementListFreeInternal list,
037: Object o) {
038: return indexOf(list, o) != -1;
039: }
040:
041: public static boolean add(ElementListFreeInternal list, Object o) {
042: list.addElement((Element) o);
043: return true; // Pues si falla se lanzaría una excepción
044: }
045:
046: public static int lastIndexOf(ElementListFreeInternal list, Object o) {
047: return list.lastIndexOfElement((Element) o);
048: }
049:
050: public static boolean remove(ElementListFreeInternal list, Object o) {
051: Element elem = (Element) o;
052: int index = list.indexOfElement(elem);
053: list.removeElement(index, elem);
054: return true; // Porque si no contiene el nodo se lanza una excepción
055: }
056:
057: public static Object[] toArray(ElementListFreeInternal list,
058: Object[] a) {
059: Element[] elems = list.getElements();
060: if (a.length < elems.length)
061: a = (Object[]) java.lang.reflect.Array.newInstance(a
062: .getClass().getComponentType(), elems.length);
063: for (int i = 0; i < elems.length; i++)
064: a[i] = elems[i];
065:
066: return a;
067: }
068:
069: public static Object get(ElementListFreeInternal list, int index) {
070: return list.getElementAt(index);
071: }
072:
073: public static Iterator iterator(ElementListFreeInternal list) {
074: return new ElementListFreeIteratorImpl(list);
075: }
076:
077: public static ListIterator listIterator(
078: ElementListFreeInternal list, int index) {
079: return new ElementListFreeListIteratorImpl(list, index);
080: }
081:
082: public static ListIterator listIterator(ElementListFreeInternal list) {
083: return new ElementListFreeListIteratorImpl(list, 0);
084: }
085:
086: public static Object remove(ElementListFreeInternal list, int index) {
087: return list.removeElementAt(index);
088: }
089:
090: public static boolean addAll(ElementListFreeInternal list,
091: int index, Collection c) {
092: for (Iterator it = c.iterator(); it.hasNext();) {
093: Element elem = (Element) it.next();
094: list.insertElementAt(index, elem);
095: index++;
096: }
097: return true;
098: }
099:
100: public static boolean containsAll(ElementListFreeInternal list,
101: Collection c) {
102: for (Iterator e = c.iterator(); e.hasNext();) {
103: if (!list.contains(e.next()))
104: return false;
105: }
106: return true;
107: }
108:
109: public static boolean addAll(ElementListFreeInternal list,
110: Collection c) {
111: for (Iterator it = c.iterator(); it.hasNext();) {
112: Element elem = (Element) it.next();
113: list.addElement(elem);
114: }
115: return true;
116: }
117:
118: public static boolean removeAll(ElementListFreeInternal list,
119: Collection c) {
120: boolean modified = false;
121: for (Iterator it = list.iterator(); it.hasNext();) {
122: if (c.contains(it.next())) {
123: it.remove();
124: modified = true;
125: }
126: }
127: return modified;
128: }
129:
130: public static boolean retainAll(ElementListFreeInternal list,
131: Collection c) {
132: boolean modified = false;
133: for (Iterator it = list.iterator(); it.hasNext();) {
134: if (!c.contains(it.next())) {
135: it.remove();
136: modified = true;
137: }
138: }
139: return modified;
140: }
141:
142: public static void add(ElementListFreeInternal list, int index,
143: Object element) {
144: list.insertElementAt(index, (Element) element);
145: }
146:
147: public static Object set(ElementListFreeInternal list, int index,
148: Object element) {
149: return list.setElementAt(index, (Element) element);
150: }
151:
152: public static Object[] toArray(ElementListFreeInternal list) {
153: return list.getElements();
154: }
155:
156: public static List subList(ElementListFreeInternal list,
157: int fromIndex, int toIndex) {
158: throw new ItsNatException("Not implemented");
159: }
160:
161: public static int size(ElementListFreeInternal list) {
162: return list.getLength();
163: }
164:
165: public static void clear(ElementListFreeInternal list) {
166: list.removeAllElements();
167: }
168: }
|