litesoft
@ HEAD
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 |
// This Source Code is in the Public Domain per: http://unlicense.org package org.litesoft.bo.views; import org.litesoft.commonfoundation.base.*; import java.io.*; public class ObjectUniqueKey implements Serializable { private static final long serialVersionUID = -1L; public static final String PREFIX = "+"; private /* final */ Type mType; private /* final */ String mID; @SuppressWarnings({"deprecation", "UnusedDeclaration"}) @Deprecated /** for Serialization */ 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: " + ClassName.simple( 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: " + ClassName.simple( 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; } } } |
Commits for litesoft/trunk/Java/core/Anywhere/src/org/litesoft/bo/views/ObjectUniqueKey.java
Revision | Author | Commited | Message |
---|---|---|---|
950 Diff | GeorgeS | Thu 19 Jun, 2014 17:57:04 +0000 | New Lines |
949 Diff | GeorgeS | Sun 08 Jun, 2014 17:19:33 +0000 | Normalization |
948 Diff | GeorgeS | Sat 07 Jun, 2014 23:42:39 +0000 | Jusefuls Formatter Updated to New Code Format |
947 Diff | GeorgeS | Fri 06 Jun, 2014 23:36:56 +0000 | Correct Spelling of package! |
939 Diff | GeorgeS | Mon 02 Jun, 2014 21:30:31 +0000 | Extracting commonfoundation |
849 Diff | GeorgeS | Tue 11 Sep, 2012 17:11:59 +0000 | Clean up serialization |
811 Diff | GeorgeS | Sat 18 Aug, 2012 13:45:18 +0000 | |
151 Diff | GeorgeS | Thu 17 Mar, 2011 04:16:22 +0000 | |
49 Diff | GeorgeS | Mon 12 Apr, 2010 02:59:10 +0000 | License Text |
2 | GeorgeS | Sun 07 Feb, 2010 12:50:58 +0000 |