PageRenderTime 25ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/com/google/appengine/datanucleus/test/HasOneToManyArrayJDO.java

http://datanucleus-appengine.googlecode.com/
Java | 206 lines | 147 code | 39 blank | 20 comment | 11 complexity | dce137988f06faa1f5c1f2c5293bfec4 MD5 | raw file
Possible License(s): Apache-2.0
  1. /**********************************************************************
  2. Copyright (c) 2009 Google Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. **********************************************************************/
  13. package com.google.appengine.datanucleus.test;
  14. import java.lang.reflect.Array;
  15. import java.util.ArrayList;
  16. import java.util.Arrays;
  17. import java.util.Collection;
  18. import java.util.List;
  19. import javax.jdo.annotations.Element;
  20. import javax.jdo.annotations.Extension;
  21. import javax.jdo.annotations.IdGeneratorStrategy;
  22. import javax.jdo.annotations.PersistenceCapable;
  23. import javax.jdo.annotations.Persistent;
  24. import javax.jdo.annotations.PrimaryKey;
  25. /**
  26. * @author Max Ross <maxr@google.com>
  27. */
  28. @PersistenceCapable(detachable = "true")
  29. public class HasOneToManyArrayJDO implements HasOneToManyJDO {
  30. @PrimaryKey
  31. @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
  32. @Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value="true")
  33. private String id;
  34. @Persistent
  35. private String val;
  36. @Persistent(mappedBy = "parent", dependentElement = "true")
  37. @Element(dependent = "true")
  38. private BidirectionalChildArrayJDO[] bidirChildren = null;
  39. @Persistent(dependentElement = "true")
  40. @Element(dependent = "true")
  41. private Flight[] flights = null;
  42. @Persistent(dependentElement = "true")
  43. @Element(dependent = "true")
  44. private HasKeyPkJDO[] hasKeyPks = null;
  45. public String getId() {
  46. return id;
  47. }
  48. private List asList(Object[] arr) {
  49. if (arr == null) {
  50. return new ArrayList<Object>();
  51. }
  52. return Arrays.asList(arr);
  53. }
  54. public List<BidirectionalChildJDO> getBidirChildren() {
  55. return asList(bidirChildren);
  56. }
  57. public void addBidirChild(BidirectionalChildJDO bidirChild) {
  58. List<BidirectionalChildJDO> list = getBidirChildren();
  59. list.add(bidirChild);
  60. bidirChildren = list.toArray(new BidirectionalChildArrayJDO[0]);
  61. }
  62. public List<Flight> getFlights() {
  63. return asList(flights);
  64. }
  65. public void addFlight(Flight flight) {
  66. List<Flight> list = getFlights();
  67. list.add(flight);
  68. flights = list.toArray(new Flight[0]);
  69. }
  70. public List<HasKeyPkJDO> getHasKeyPks() {
  71. return asList(hasKeyPks);
  72. }
  73. public void addHasKeyPk(HasKeyPkJDO hasKeyPk) {
  74. List<HasKeyPkJDO> list = getHasKeyPks();
  75. list.add(hasKeyPk);
  76. hasKeyPks = list.toArray(new HasKeyPkJDO[0]);
  77. }
  78. public String getVal() {
  79. return val;
  80. }
  81. public void setVal(String val) {
  82. this.val = val;
  83. }
  84. public void nullBidirChildren() {
  85. bidirChildren = null;
  86. }
  87. public void nullFlights() {
  88. flights = null;
  89. }
  90. public void nullHasKeyPks() {
  91. hasKeyPks = null;
  92. }
  93. public void clearBidirChildren() {
  94. if (bidirChildren != null) {
  95. for (int i = 0; i < bidirChildren.length; i++) {
  96. bidirChildren[i] = null;
  97. }
  98. }
  99. }
  100. public void clearFlights() {
  101. if (flights != null) {
  102. for (int i = 0; i < flights.length; i++) {
  103. flights[i] = null;
  104. }
  105. }
  106. }
  107. public void clearHasKeyPks() {
  108. if (hasKeyPks != null) {
  109. for (int i = 0; i < hasKeyPks.length; i++) {
  110. hasKeyPks[i] = null;
  111. }
  112. }
  113. }
  114. private Object[] copyAndSetAtPos(Object[] arr, Object obj, int pos) {
  115. // datanuc can't detect changes to array elements so we need
  116. // to replace the whole array
  117. Object[] copy = (Object[]) Array.newInstance(obj.getClass(), arr.length);
  118. System.arraycopy(arr, 0, copy, 0, arr.length);
  119. copy[pos] = obj;
  120. return copy;
  121. }
  122. public void addBidirChildAtPosition(BidirectionalChildJDO bidir, int pos) {
  123. bidirChildren = (BidirectionalChildArrayJDO[]) copyAndSetAtPos(bidirChildren, bidir, pos);
  124. }
  125. public void addFlightAtPosition(Flight f, int pos) {
  126. flights = (Flight[]) copyAndSetAtPos(flights, f, pos);
  127. }
  128. public void addHasKeyPkAtPosition(HasKeyPkJDO hasKeyPk, int pos) {
  129. hasKeyPks = (HasKeyPkJDO[]) copyAndSetAtPos(hasKeyPks, hasKeyPk, pos);
  130. }
  131. public void removeBidirChildAtPosition(int i) {
  132. bidirChildren = (BidirectionalChildArrayJDO[]) copyAndSetAtPos(bidirChildren, null, i);
  133. }
  134. public void removeFlightAtPosition(int i) {
  135. flights = (Flight[]) copyAndSetAtPos(flights, null, i);
  136. }
  137. public void removeHasKeyPkAtPosition(int i) {
  138. hasKeyPks = (HasKeyPkJDO[]) copyAndSetAtPos(hasKeyPks, null, i);
  139. }
  140. private Object[] addAtPosition(int i, Object[] arr, Object newElement) {
  141. List<Object> list =
  142. new ArrayList<Object>(Arrays.asList(arr));
  143. list.add(i, newElement);
  144. return list.toArray((Object[]) Array.newInstance(newElement.getClass(), list.size()));
  145. }
  146. public void addAtPosition(int i, BidirectionalChildJDO bidir) {
  147. bidirChildren = (BidirectionalChildArrayJDO[]) addAtPosition(i, bidirChildren, bidir);
  148. }
  149. public void addAtPosition(int i, Flight f) {
  150. flights = (Flight[]) addAtPosition(i, flights, f);
  151. }
  152. public void addAtPosition(int i, HasKeyPkJDO hasKeyPk) {
  153. hasKeyPks = (HasKeyPkJDO[]) addAtPosition(i, hasKeyPks, hasKeyPk);
  154. }
  155. public void removeFlights(Collection<Flight> flights) {
  156. throw new UnsupportedOperationException();
  157. }
  158. public void removeBidirChildren(Collection<BidirectionalChildJDO> bidirChildren) {
  159. throw new UnsupportedOperationException();
  160. }
  161. public void setBidirChildren(Collection<BidirectionalChildJDO> childList) {
  162. throw new UnsupportedOperationException();
  163. }
  164. }