1 24 package javax.jcr; 25 26 import javax.jcr.util.ISO8601; 27 import java.util.Calendar ; 28 29 35 public class StringValue extends BaseValue { 36 37 public static final int TYPE = PropertyType.STRING; 38 39 private final String text; 40 41 46 public StringValue(String text) { 47 super(TYPE); 48 this.text = text; 49 } 50 51 62 public boolean equals(Object obj) { 63 if (this == obj) { 64 return true; 65 } 66 if (obj instanceof StringValue) { 67 StringValue other = (StringValue) obj; 68 if (text == other.text) { 69 return true; 70 } else if (text != null && other.text != null) { 71 return text.equals(other.text); 72 } 73 } 74 return false; 75 } 76 77 81 public Calendar getDate() throws ValueFormatException, IllegalStateException , RepositoryException { 82 setValueConsumed(); 83 84 if (text != null) { 85 Calendar cal = ISO8601.parse(text); 86 if (cal == null) { 87 throw new ValueFormatException("not a valid date format"); 88 } else { 89 return cal; 90 } 91 } else { 92 throw new ValueFormatException("empty value"); 93 } 94 } 95 96 99 public long getLong() throws ValueFormatException, IllegalStateException , RepositoryException { 100 setValueConsumed(); 101 102 if (text != null) { 103 try { 104 return Long.parseLong(text); 105 } catch (NumberFormatException e) { 106 throw new ValueFormatException("conversion to long failed", e); 107 } 108 } else { 109 throw new ValueFormatException("empty value"); 110 } 111 } 112 113 116 public boolean getBoolean() throws ValueFormatException, IllegalStateException , RepositoryException { 117 setValueConsumed(); 118 119 if (text != null) { 120 return Boolean.valueOf(text).booleanValue(); 121 } else { 122 throw new ValueFormatException("empty value"); 123 } 124 } 125 126 129 public double getDouble() throws ValueFormatException, IllegalStateException , RepositoryException { 130 setValueConsumed(); 131 132 if (text != null) { 133 try { 134 return Double.parseDouble(text); 135 } catch (NumberFormatException e) { 136 throw new ValueFormatException("conversion to double failed", e); 137 } 138 } else { 139 throw new ValueFormatException("empty value"); 140 } 141 } 142 143 146 public String getString() throws ValueFormatException, IllegalStateException , RepositoryException { 147 setValueConsumed(); 148 149 if (text != null) { 150 return text; 151 } else { 152 throw new ValueFormatException("empty value"); 153 } 154 } 155 } 156 | Popular Tags |