1 /* 2 Copyright (c) 2011 James Ahlborn 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 17 package com.healthmarketscience.jackcess.complex; 18 19 import java.io.IOException; 20 import java.io.ObjectStreamException; 21 22 import com.healthmarketscience.jackcess.Column; 23 import com.healthmarketscience.jackcess.RowId; 24 import com.healthmarketscience.jackcess.impl.complex.ComplexColumnInfoImpl; 25 26 /** 27 * Base interface for a value in a complex column (where there may be multiple 28 * values for a single row in the main table). 29 * 30 * @author James Ahlborn 31 */ 32 public interface ComplexValue 33 { 34 /** 35 * Returns the unique identifier of this complex value (this value is unique 36 * among all values in all rows of the main table). 37 * 38 * @return the current id or {@link ComplexColumnInfoImpl#INVALID_ID} for a new, 39 * unsaved value. 40 */ 41 public Id getId(); 42 43 /** 44 * Called once when a new ComplexValue is saved to set the new unique 45 * identifier. 46 */ 47 public void setId(Id newId); 48 49 /** 50 * Returns the foreign key identifier for this complex value (this value is 51 * the same for all values in the same row of the main table). 52 * 53 * @return the current id or {@link ComplexColumnInfoImpl#INVALID_FK} 54 * for a new, unsaved value. 55 */ 56 public ComplexValueForeignKey getComplexValueForeignKey(); 57 58 public void setComplexValueForeignKey(ComplexValueForeignKey complexValueFk); 59 60 /** 61 * @return the column in the main table with which this complex value is 62 * associated 63 */ 64 public Column getColumn(); 65 66 /** 67 * Writes any updated data for this complex value to the database. 68 */ 69 public void update() throws IOException; 70 71 /** 72 * Deletes the data for this complex value from the database. 73 */ 74 public void delete() throws IOException; 75 76 77 /** 78 * Identifier for a ComplexValue. Only valid for comparing complex values 79 * for the same column. 80 */ 81 public abstract class Id extends Number 82 { 83 private static final long serialVersionUID = 20130318L; 84 85 @Override 86 public byte byteValue() { 87 return (byte)get(); 88 } 89 90 @Override 91 public short shortValue() { 92 return (short)get(); 93 } 94 95 @Override 96 public int intValue() { 97 return get(); 98 } 99 100 @Override 101 public long longValue() { 102 return get(); 103 } 104 105 @Override 106 public float floatValue() { 107 return get(); 108 } 109 110 @Override 111 public double doubleValue() { 112 return get(); 113 } 114 115 @Override 116 public int hashCode() { 117 return get(); 118 } 119 120 @Override 121 public boolean equals(Object o) { 122 return ((this == o) || 123 ((o != null) && (getClass() == o.getClass()) && 124 (get() == ((Id)o).get()))); 125 } 126 127 @Override 128 public String toString() { 129 return String.valueOf(get()); 130 } 131 132 protected final Object writeReplace() throws ObjectStreamException { 133 // if we are going to serialize this ComplexValue.Id, convert it back to 134 // a normal Integer (in case it is restored outside of the context of 135 // jackcess) 136 return Integer.valueOf(get()); 137 } 138 139 /** 140 * Returns the unique identifier of this complex value (this value is unique 141 * among all values in all rows of the main table for the complex column). 142 */ 143 public abstract int get(); 144 145 /** 146 * Returns the rowId of this ComplexValue within the secondary table. 147 */ 148 public abstract RowId getRowId(); 149 } 150 }