PageRenderTime 28ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/0.9.25/core/src/main/java/com/orientechnologies/orient/core/db/object/OLazyObjectList.java

http://orient.googlecode.com/
Java | 235 lines | 172 code | 42 blank | 21 comment | 19 complexity | 628188acb30fdceb2ecbc1b82f50bd30 MD5 | raw file
Possible License(s): Apache-2.0, AGPL-3.0
  1. /*
  2. * Copyright 1999-2010 Luca Garulli (l.garulli--at--orientechnologies.com)
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.orientechnologies.orient.core.db.object;
  17. import java.io.Serializable;
  18. import java.util.ArrayList;
  19. import java.util.Collection;
  20. import java.util.Iterator;
  21. import java.util.List;
  22. import java.util.ListIterator;
  23. import com.orientechnologies.orient.core.db.ODatabasePojoAbstract;
  24. import com.orientechnologies.orient.core.db.graph.ODatabaseGraphTx;
  25. import com.orientechnologies.orient.core.id.ORID;
  26. import com.orientechnologies.orient.core.record.ORecord;
  27. import com.orientechnologies.orient.core.record.ORecordInternal;
  28. import com.orientechnologies.orient.core.record.impl.ODocument;
  29. @SuppressWarnings({ "unchecked" })
  30. public class OLazyObjectList<TYPE> implements List<TYPE>, Serializable {
  31. private static final long serialVersionUID = 289711963195698937L;
  32. private ORecord<?> sourceRecord;
  33. private final ArrayList<Object> list = new ArrayList<Object>();
  34. private ODatabasePojoAbstract<TYPE> database;
  35. private String fetchPlan;
  36. private boolean converted = false;
  37. private boolean convertToRecord = true;
  38. public OLazyObjectList(final ODatabaseGraphTx iDatabase, final ORecord<?> iSourceRecord, final Collection<?> iSourceList) {
  39. this((ODatabasePojoAbstract<TYPE>) iDatabase, iSourceRecord, iSourceList);
  40. this.sourceRecord = iSourceRecord;
  41. }
  42. public OLazyObjectList(final ODatabasePojoAbstract<TYPE> iDatabase, final ORecord<?> iSourceRecord,
  43. final Collection<?> iSourceList) {
  44. this(iDatabase);
  45. this.sourceRecord = iSourceRecord;
  46. if (iSourceList != null)
  47. list.addAll(iSourceList);
  48. }
  49. public OLazyObjectList(final ODatabasePojoAbstract<TYPE> iDatabase) {
  50. this.database = iDatabase;
  51. }
  52. public Iterator<TYPE> iterator() {
  53. return new OLazyObjectIterator<TYPE>(database, sourceRecord, list.iterator(), convertToRecord);
  54. }
  55. public boolean contains(final Object o) {
  56. convertAll();
  57. return list.contains(o);
  58. }
  59. public boolean add(TYPE element) {
  60. if (converted && element instanceof ORID)
  61. converted = false;
  62. setDirty();
  63. return list.add(element);
  64. }
  65. public void add(int index, TYPE element) {
  66. if (converted && element instanceof ORID)
  67. converted = false;
  68. setDirty();
  69. list.add(index, element);
  70. }
  71. public TYPE get(final int index) {
  72. convert(index);
  73. return (TYPE) list.get(index);
  74. }
  75. public int indexOf(final Object o) {
  76. convertAll();
  77. return list.indexOf(o);
  78. }
  79. public int lastIndexOf(final Object o) {
  80. convertAll();
  81. return list.lastIndexOf(o);
  82. }
  83. public Object[] toArray() {
  84. convertAll();
  85. return list.toArray();
  86. }
  87. public <T> T[] toArray(final T[] a) {
  88. convertAll();
  89. return list.toArray(a);
  90. }
  91. public int size() {
  92. return list.size();
  93. }
  94. public boolean isEmpty() {
  95. return list.isEmpty();
  96. }
  97. public boolean remove(Object o) {
  98. convertAll();
  99. setDirty();
  100. return list.remove(o);
  101. }
  102. public boolean containsAll(Collection<?> c) {
  103. convertAll();
  104. return list.containsAll(c);
  105. }
  106. public boolean addAll(Collection<? extends TYPE> c) {
  107. setDirty();
  108. return list.addAll(c);
  109. }
  110. public boolean addAll(int index, Collection<? extends TYPE> c) {
  111. setDirty();
  112. return list.addAll(index, c);
  113. }
  114. public boolean removeAll(Collection<?> c) {
  115. convertAll();
  116. setDirty();
  117. return list.removeAll(c);
  118. }
  119. public boolean retainAll(Collection<?> c) {
  120. convertAll();
  121. setDirty();
  122. return list.retainAll(c);
  123. }
  124. public void clear() {
  125. setDirty();
  126. list.clear();
  127. }
  128. public TYPE set(int index, TYPE element) {
  129. convert(index);
  130. setDirty();
  131. return (TYPE) list.set(index, element);
  132. }
  133. public TYPE remove(int index) {
  134. convert(index);
  135. setDirty();
  136. return (TYPE) list.remove(index);
  137. }
  138. public ListIterator<TYPE> listIterator() {
  139. return (ListIterator<TYPE>) list.listIterator();
  140. }
  141. public ListIterator<TYPE> listIterator(int index) {
  142. return (ListIterator<TYPE>) list.listIterator(index);
  143. }
  144. public List<TYPE> subList(int fromIndex, int toIndex) {
  145. return (List<TYPE>) list.subList(fromIndex, toIndex);
  146. }
  147. public String getFetchPlan() {
  148. return fetchPlan;
  149. }
  150. public OLazyObjectList<TYPE> setFetchPlan(String fetchPlan) {
  151. this.fetchPlan = fetchPlan;
  152. return this;
  153. }
  154. public boolean isConvertToRecord() {
  155. return convertToRecord;
  156. }
  157. public void setConvertToRecord(boolean convertToRecord) {
  158. this.convertToRecord = convertToRecord;
  159. }
  160. public void convertAll() {
  161. if (converted || !convertToRecord)
  162. return;
  163. for (int i = 0; i < size(); ++i)
  164. convert(i);
  165. converted = true;
  166. }
  167. public void setDirty() {
  168. if (sourceRecord != null)
  169. sourceRecord.setDirty();
  170. }
  171. public void assignDatabase(final ODatabasePojoAbstract<TYPE> iDatabase) {
  172. if (database == null || database.isClosed()) {
  173. database = iDatabase;
  174. }
  175. }
  176. /**
  177. * Convert the item requested.
  178. *
  179. * @param iIndex
  180. * Position of the item to convert
  181. */
  182. private void convert(final int iIndex) {
  183. if (converted || !convertToRecord)
  184. return;
  185. final Object o = list.get(iIndex);
  186. if (o != null && o instanceof ODocument)
  187. list.set(iIndex, database.getUserObjectByRecord((ORecordInternal<?>) o, fetchPlan));
  188. }
  189. @Override
  190. public String toString() {
  191. return list.toString();
  192. }
  193. }