001: /*
002: * Copyright 2006 Davide Deidda
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: /*
018: * PersistentList.java
019: *
020: * Created on 1 maggio 2005, 10.55
021: */
022:
023: package it.biobytes.ammentos.util;
024:
025: import it.biobytes.ammentos.*;
026:
027: import java.util.*;
028:
029: /**
030: * A List implementation whith persistent entries. This means that when a add()
031: * call is performed he added Es is also persisted, and when remove() method
032: * is called the E is also removed from the database
033: *
034: * @author Davide Deidda
035: */
036: public class UpdatableList<E> implements List<E> {
037: private List<E> m_peer;
038:
039: public UpdatableList(List<E> list) {
040: m_peer = list;
041: }
042:
043: public boolean remove(Object o) {
044: boolean res;
045: try {
046: Ammentos.delete(o);
047: res = m_peer.remove(o);
048: } catch (PersistenceException e) {
049: res = false;
050: }
051: return res;
052: }
053:
054: public boolean add(E o) {
055: boolean res;
056: try {
057: Ammentos.save(o);
058: res = m_peer.add(o);
059: } catch (PersistenceException e) {
060: res = false;
061: }
062: return res;
063: }
064:
065: public boolean contains(Object o) {
066: return m_peer.contains(o);
067: }
068:
069: public int indexOf(Object o) {
070: return m_peer.indexOf(o);
071: }
072:
073: public int lastIndexOf(Object o) {
074: return m_peer.lastIndexOf(o);
075: }
076:
077: public E remove(int index) {
078: E res = m_peer.remove(index);
079: try {
080: Ammentos.delete(res);
081: } catch (Exception e) {
082: }
083: return res;
084: }
085:
086: public ListIterator<E> listIterator(int index) {
087: return m_peer.listIterator(index);
088: }
089:
090: public E get(int index) {
091: return m_peer.get(index);
092: }
093:
094: public boolean addAll(int index, Collection<? extends E> c) {
095: return m_peer.addAll(index, c);
096: }
097:
098: public E set(int index, E element) {
099: E res = m_peer.set(index, element);
100: try {
101: Ammentos.save(res);
102: } catch (Exception e) {
103: }
104: return res;
105: }
106:
107: public void add(int index, E element) {
108: try {
109: Ammentos.save(element);
110: } catch (Exception e) {
111: }
112: m_peer.add(index, element);
113: }
114:
115: public boolean retainAll(Collection<?> c) {
116: return m_peer.retainAll(c);
117: }
118:
119: public boolean removeAll(Collection<?> c) {
120: return m_peer.removeAll(c);
121: }
122:
123: public boolean addAll(Collection<? extends E> c) {
124: return m_peer.addAll(c);
125: }
126:
127: public boolean containsAll(Collection<?> c) {
128: return m_peer.containsAll(c);
129: }
130:
131: public <T> T[] toArray(T[] a) {
132: return m_peer.toArray(a);
133: }
134:
135: public Object[] toArray() {
136: return m_peer.toArray();
137: }
138:
139: public List<E> subList(int fromIndex, int toIndex) {
140: return m_peer.subList(fromIndex, toIndex);
141: }
142:
143: public int size() {
144: return m_peer.size();
145: }
146:
147: public void clear() {
148: m_peer.clear();
149: }
150:
151: public boolean isEmpty() {
152: return m_peer.isEmpty();
153: }
154:
155: public Iterator<E> iterator() {
156: return m_peer.iterator();
157: }
158:
159: public ListIterator<E> listIterator() {
160: return m_peer.listIterator();
161: }
162:
163: }
|