001 package com.hammurapi.reasoning.impl;
002
003 import java.util.ArrayList;
004 import java.util.Collection;
005 import java.util.List;
006 import java.util.ListIterator;
007
008 import com.hammurapi.reasoning.Handle;
009 import com.hammurapi.reasoning.ReasoningException;
010
011 public class HandleList<E> extends HandleCollection<E> implements List<E> {
012
013 public HandleList(KnowledgeBase<E> kb, List<Handle<E>> handleCollection) {
014 super(kb, handleCollection);
015 }
016
017 @Override
018 public void add(int index, E element) {
019 try {
020 ((List<Handle<E>>) handleCollection).add(index, knowledgeBase.put(element));
021 } catch (ReasoningException e) {
022 throw new RuntimeReasoningException(e);
023 }
024 }
025
026 @Override
027 public boolean addAll(int index, Collection<? extends E> c) {
028 ArrayList<Handle<E>> hc = new ArrayList<Handle<E>>();
029 for (E e: c) {
030 try {
031 hc.add(knowledgeBase.put(e));
032 } catch (ReasoningException ex) {
033 throw new RuntimeReasoningException(ex);
034 }
035 }
036 return ((List<Handle<E>>) handleCollection).addAll(index, hc);
037 }
038
039 @Override
040 public E get(int index) {
041 return ((HandleImpl<E> )((List<Handle<E>>) handleCollection).get(index)).get();
042 }
043
044 @Override
045 public int indexOf(Object o) {
046 Handle<E> h = knowledgeBase.getHandle(o);
047 return h==null ? -1 : ((List<Handle<E>>) handleCollection).indexOf(h);
048 }
049
050 @Override
051 public int lastIndexOf(Object o) {
052 Handle<E> h = knowledgeBase.getHandle(o);
053 return h==null ? -1 : ((List<Handle<E>>) handleCollection).lastIndexOf(h);
054 }
055
056 private class HandleIterator implements ListIterator<E> {
057
058 ListIterator<Handle<E>> master;
059
060 HandleIterator(ListIterator<Handle<E>> master) {
061 this.master = master;
062 }
063
064 @Override
065 public void add(E e) {
066 try {
067 master.add(knowledgeBase.put(e));
068 } catch (ReasoningException ex) {
069 throw new RuntimeReasoningException(ex);
070 }
071 }
072
073 @Override
074 public boolean hasNext() {
075 return master.hasNext();
076 }
077
078 @Override
079 public boolean hasPrevious() {
080 return master.hasPrevious();
081 }
082
083 @Override
084 public E next() {
085 return ((HandleImpl<E>) master.next()).get();
086 }
087
088 @Override
089 public int nextIndex() {
090 return master.nextIndex();
091 }
092
093 @Override
094 public E previous() {
095 return ((HandleImpl<E>) master.previous()).get();
096 }
097
098 @Override
099 public int previousIndex() {
100 return master.previousIndex();
101 }
102
103 @Override
104 public void remove() {
105 master.remove();
106 }
107
108 @Override
109 public void set(E e) {
110 try {
111 master.set(knowledgeBase.put(e));
112 } catch (ReasoningException ex) {
113 throw new RuntimeReasoningException(ex);
114 }
115 }
116 };
117
118
119 @Override
120 public ListIterator<E> listIterator() {
121 return new HandleIterator(((List<Handle<E>>) handleCollection).listIterator());
122 }
123
124 @Override
125 public ListIterator<E> listIterator(int index) {
126 return new HandleIterator(((List<Handle<E>>) handleCollection).listIterator(index));
127 }
128
129 @Override
130 public E remove(int index) {
131 try {
132 return (E) knowledgeBase.get(((List<Handle<E>>) handleCollection).remove(index));
133 } catch (ReasoningException e) {
134 throw new RuntimeReasoningException(e);
135 }
136 }
137
138 @SuppressWarnings("unchecked")
139 @Override
140 public E set(int index, E element) {
141 try {
142 Handle h = ((List<Handle<E>>) handleCollection).set(index, knowledgeBase.put(element));
143 return (E) knowledgeBase.get(h);
144 } catch (ReasoningException e) {
145 throw new RuntimeReasoningException(e);
146 }
147 }
148
149 @Override
150 public List<E> subList(int fromIndex, int toIndex) {
151 return new HandleList<E>(knowledgeBase, ((List<Handle<E>>) handleCollection).subList(fromIndex, toIndex));
152 }
153
154
155 }