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