01: /*
02: * Copyright (C) 2006 Joe Walnes.
03: * Copyright (C) 2007 XStream Committers.
04: * All rights reserved.
05: *
06: * The software in this package is published under the terms of the BSD
07: * style license a copy of which has been included with this distribution in
08: * the LICENSE.txt file.
09: *
10: * Created on 06. July 2006 by Guilherme Silveira
11: */
12: package com.thoughtworks.xstream.persistence;
13:
14: import java.util.AbstractList;
15:
16: /**
17: * A persistent list implementation backed on a XmlMap.
18: *
19: * @author Guilherme Silveira
20: */
21: public class XmlArrayList extends AbstractList {
22:
23: private final XmlMap map;
24:
25: public XmlArrayList(StreamStrategy streamStrategy) {
26: this .map = new XmlMap(streamStrategy);
27: }
28:
29: public int size() {
30: return map.size();
31: }
32:
33: public Object set(int index, Object element) {
34: rangeCheck(index);
35: Object value = get(index);
36: map.put(String.valueOf(index), element);
37: return value;
38: }
39:
40: public void add(int index, Object element) {
41: int size = size();
42: if (index >= (size + 1) || index < 0) {
43: throw new IndexOutOfBoundsException("Index: " + index
44: + ", Size: " + size);
45: }
46: int to = index != size ? index - 1 : index;
47: for (int i = size; i > to; i--) {
48: map.put(String.valueOf(i + 1), map.get(String.valueOf(i)));
49: }
50: map.put(String.valueOf(index), element);
51: }
52:
53: private void rangeCheck(int index) {
54: int size = size();
55: if (index >= size || index < 0) {
56: throw new IndexOutOfBoundsException("Index: " + index
57: + ", Size: " + size);
58: }
59: }
60:
61: public Object get(int index) {
62: rangeCheck(index);
63: return map.get(String.valueOf(index));
64: }
65:
66: public Object remove(int index) {
67: int size = size();
68: rangeCheck(index);
69: Object value = map.get(String.valueOf(index));
70: for (int i = index; i < size - 1; i++) {
71: map.put(String.valueOf(i), map.get(String.valueOf(i + 1)));
72: }
73: map.remove(String.valueOf(size - 1));
74: return value;
75: }
76:
77: }
|