00001
00002 package de.aitools.aq.value.pair;
00003
00004 import java.io.PrintStream;
00005 import java.util.Scanner;
00006
00007 import com.sun.jna.Memory;
00008 import com.sun.jna.Native;
00009 import com.sun.jna.Pointer;
00010
00011 import de.aitools.aq.check.A;
00012 import de.aitools.aq.value.Value;
00013
00014 public class IntString extends Value {
00015
00016 public static final int NATIVE_TYPE_ID = 251;
00017
00018 private static final int OFFSET_E2 = Value.SIZEOF_INT;
00019
00020 private static final int SIZEOF_STRING_SIZE = Value.SIZEOF_SHORT;
00021
00022 public IntString() {
00023 e2 = new String();
00024 }
00025
00026 public IntString(int e1, String e2) {
00027 set(e1, e2);
00028 }
00029
00030 public IntString(IntString value) {
00031 set(value.e1, value.e2);
00032 }
00033
00034 public int getE1() {
00035 return e1;
00036 }
00037
00038 public void setE1(int e1) {
00039 this.e1 = e1;
00040 }
00041
00042 public String getE2() {
00043 return e2;
00044 }
00045
00046 public void setE2(String e2) {
00047 A.CHECK_NOT_NULL(e2);
00048 this.e2 = e2;
00049 }
00050
00051 public void set(int e1, String e2) {
00052 setE1(e1);
00053 setE2(e2);
00054 }
00055
00056 @Override
00057 public void copyFrom(Pointer buffer) {
00058 e1 = buffer.getInt(0);
00059 short stringSize = buffer.getShort(OFFSET_E2);
00060 e2 = Native.toString(buffer.getByteArray(
00061 OFFSET_E2 + SIZEOF_STRING_SIZE, stringSize));
00062 }
00063
00064 @Override
00065 public void copyTo(Pointer buffer) {
00066 buffer.setInt(0, e1);
00067 byte[] bytes = Native.toByteArray(e2);
00068 A.CHECK(bytes.length <= Short.MAX_VALUE, "Too long string");
00069 buffer.setShort(OFFSET_E2, (short) bytes.length);
00070 buffer.write(OFFSET_E2 + SIZEOF_STRING_SIZE, bytes, 0, bytes.length);
00071 }
00072
00073 @Override
00074 public boolean parseFrom(Scanner sc) {
00075 if (sc.hasNextInt()) e1 = sc.nextInt();
00076 else return false;
00077 if (sc.hasNext()) e2 = sc.next();
00078 else return false;
00079 return true;
00080 }
00081
00082 @Override
00083 public PrintStream printTo(PrintStream stream) {
00084 A.CHECK(e2.indexOf('\t') == -1, "No tabulator allowed in string");
00085 stream.print(e1);
00086 stream.print(DELIMITER_C);
00087 stream.print(e2);
00088 return stream;
00089 }
00090
00091 @Override
00092 public Memory getSharedMemory() {
00093 return new Memory(getSerializedSize());
00094 }
00095
00096 @Override
00097 public int getSerializedSize() {
00098 return OFFSET_E2 + SIZEOF_STRING_SIZE + Native.toByteArray(e2).length;
00099 }
00100
00101 @Override
00102 public int hashCode() {
00103 final int prime = 31;
00104 int result = 1;
00105 result = prime * result + e1;
00106 result = prime * result + ((e2 == null) ? 0 : e2.hashCode());
00107 return result;
00108 }
00109
00110 @Override
00111 public boolean equals(Object obj) {
00112 if (this == obj)
00113 return true;
00114 if (obj == null)
00115 return false;
00116 if (getClass() != obj.getClass())
00117 return false;
00118 IntString other = (IntString) obj;
00119 if (e1 != other.e1)
00120 return false;
00121 if (e2 == null) {
00122 if (other.e2 != null)
00123 return false;
00124 } else if (!e2.equals(other.e2))
00125 return false;
00126 return true;
00127 }
00128
00129 private int e1;
00130 private String e2;
00131 }