litesoft
@ 811
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt package org.litesoft.bo.views; import java.io.*; import org.litesoft.core.typeutils.*; public class ObjectUniqueKey implements Serializable { public static final String PREFIX = "+"; static final long serialVersionUID = -1L; private Type mType; private String mID; @Deprecated protected ObjectUniqueKey() { } private ObjectUniqueKey( Type pType, Object pID ) { mType = pType; mID = pType.validate( pID ).toString(); } public static ObjectUniqueKey createExisting( Long pLong ) { return new ObjectUniqueKey( Type.Long, pLong ); } public static ObjectUniqueKey createNew( Long pLong ) { return new ObjectUniqueKey( Type.NewLong, pLong ); } public static ObjectUniqueKey createExisting( String pString ) { return new ObjectUniqueKey( Type.String, pString ); } public static ObjectUniqueKey createNew( String pString ) { return new ObjectUniqueKey( Type.NewString, pString ); } public Type getType() { return mType; } public String getID() { return mID; } public Object getParsedID() { return mType.parse( mID ); } public boolean isForNewObject() { return mType.isForNew(); } public final boolean equals( Object pThem ) { return (this == pThem) || ((pThem instanceof ObjectUniqueKey) && equalsNotNullNotUs( (ObjectUniqueKey) pThem )); } public final boolean equals( ObjectUniqueKey pThem ) { return (this == pThem) || ((pThem != null) && equalsNotNullNotUs( pThem )); } private boolean equalsNotNullNotUs( ObjectUniqueKey pThem ) { return this.mType.equals( pThem.mType ) && this.mID.equals( pThem.mID ); } public final int hashCode() { return 31 * mType.hashCode() + mID.hashCode(); } public final String toString() { return PREFIX + mType.getTypeID() + mID; } public static ObjectUniqueKey fromString( String pToString ) throws IllegalArgumentException { String zTypeStr = ""; if ( pToString != null ) { if ( (pToString = pToString.trim()).startsWith( PREFIX ) ) { String zRest = pToString.substring( PREFIX.length() ).trim(); for ( Type zType : Type.values() ) { String zTypeID = zType.getTypeID(); if ( zRest.startsWith( zTypeID ) ) { zTypeStr = zType.toString() + " "; Object zID = zType.parse( zRest.substring( zTypeID.length() ) ); if ( zID != null ) { return new ObjectUniqueKey( zType, zID ); } } } } } throw new IllegalArgumentException( "Unacceptable encoding of " + zTypeStr + "ObjectUniqueKey: '" + pToString + "'" ); } public enum Type { Long( Long.class, "L", false ) { public Object validate( Object pID ) { return validateLong( pID ); } public Object parse( String pID ) { return parseLong( pID ); } }, NewLong( Long.class, "L", true ) { public Object validate( Object pID ) { return validateLong( pID ); } public Object parse( String pID ) { return parseLong( pID ); } }, String( String.class, "S", false ) { public Object validate( Object pID ) { return validateString( pID ); } public Object parse( String pID ) { return parseString( pID ); } }, NewString( String.class, "S", true ) { public Object validate( Object pID ) { return validateString( pID ); } public Object parse( String pID ) { return parseString( pID ); } }; private Class<?> mType; private String mTypeID; private boolean mForNew; private Type( Class<?> pType, String pTypeID, boolean pForNew ) { mType = pType; mTypeID = (pForNew ? "!" + pTypeID + ":" : pTypeID + ":"); mForNew = pForNew; } /** * @return the {@link Class#getName()} of the type */ public Class<?> getType() { return mType; } public String getTypeID() { return mTypeID; } public boolean isForNew() { return mForNew; } abstract public Object validate( Object pID ); abstract public Object parse( String pID ); public String validateString( Object pID ) { if ( pID instanceof String ) { String zID = (String) pID; if ( (zID = zID.trim()).length() != 0 ) { return zID; } } throw new IllegalArgumentException( "Unacceptable ObjectUniqueKey ID, expected a String. but was: " + Objects.justClassNameOf( pID ) ); } public String parseString( String pID ) { if ( pID != null ) { if ( (pID = pID.trim()).length() != 0 ) { return pID; } } return null; } public Long validateLong( Object pID ) { if ( pID instanceof Long ) { return (Long) pID; } throw new IllegalArgumentException( "Unacceptable ObjectUniqueKey ID, expected a Long. but was: " + Objects.justClassNameOf( pID ) ); } public Long parseLong( String pID ) { if ( pID != null ) { if ( (pID = pID.trim()).length() != 0 ) { try { return new Long( pID ); } catch ( NumberFormatException e ) { // fall thru... } } } return null; } } } |