// XAAL toolkit // Copyright (C) 2009 Ville Karavirta // // 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 3 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, see . package xaal.objects.structures; import java.util.ArrayList; import java.util.Iterator; import xaal.objects.Xaal; import xaal.util.XaalConstants; public class Array extends DataStructure { private boolean indexed = true; private String orientation; private ArrayList l; public Array(Xaal xaalDoc) { super(xaalDoc, DataStructure.ARRAY_TYPE); l = new ArrayList(); } public Array(Xaal xaalDoc, String id) { super(xaalDoc, DataStructure.ARRAY_TYPE, id); l = new ArrayList(); } /** * @return Returns the indexed. */ public boolean isIndexed() { return indexed; } /** * @param indexed The indexed to set. */ public void setIndexed(boolean indexed) { this.indexed = indexed; } /** * @return Returns the size. */ public int getSize() { return l.size(); } /** * @param size The size to set. */ public void setSize(int size) { changeSize(size); } private void changeSize(int size) { if (size < 0 ) { // TODO Common error logging - negative size of an array } else if (l.isEmpty()) { l = new ArrayList(size); for (int i=0; i < size; i++) l.add(null); } else if (size > l.size()) { ArrayList a = new ArrayList(); for (int i=0; i < size; i++) a.add(null); for (int i=0; i < l.size(); i++) a.set(i, l.get(i)); l = a; } } public void addNode(Node n) { if (n instanceof Index) { addIndex((Index)n); } else { // TODO Common error logging - can't add nodes to array return; } } public void addIndex(Index i) { super.addNode(i); if (i.getIndex() == -1) { if (l.size() == 0) { i.setIndex(0); l.add(i); } else { int pos = -1; Iterator iter = l.iterator(); while (iter.hasNext() && iter.next() != null) pos++; if (pos == -1) { i.setIndex(l.size()); l.add(i); } else if (pos+1 >= l.size()) { i.setIndex(pos + 1); l.add(i); } else { i.setIndex(pos + 1); l.set(pos + 1, i); } l.add(i); } } else { changeSize(i.getIndex() + 1); l.set(i.getIndex(), i); } } public Index getIndex(int index) { if (index > l.size()) return null; return (Index) l.get(index); } public void setIndex(Index i, int index) { i.setIndex(index); addIndex(i); } public Iterator getIndexes() { return getNodes(); } /** * @return Returns the orientation. */ public String getOrientation() { if (orientation == null) return XaalConstants.DEFAULT_PROPERTY_ORIENTATION; return orientation; } /** * @param orientation The orientation to set. */ public void setOrientation(String orientation) { if (!orientation.equals("vertical") && !orientation.equals("horizontal")) { // TODO Common error logging - unknown orientation } this.orientation = orientation; } }