001: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
002: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
003: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
004: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
005: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
006: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
007: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
008: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
009: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
010: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
011: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
012: // POSSIBILITY OF SUCH DAMAGE.
013: //
014: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
015: package designlibrary.core;
016:
017: import java.io.PrintWriter;
018: import java.util.ArrayList;
019: import java.util.HashSet;
020: import java.util.Properties;
021:
022: import com.metaboss.enterprise.bs.BSException;
023: import com.metaboss.javatemplate.JavaTemplate;
024: import com.metaboss.javatemplate.JavaTemplateContext;
025: import com.metaboss.javatemplate.JavaTemplateException;
026: import com.metaboss.sdlctools.models.metabossmodel.datadictionarymodel.DataType;
027: import com.metaboss.sdlctools.services.codegeneration.CodeGenerationStylesheetAccessor;
028: import com.metaboss.sdlctools.services.codegenerationstylesheet.STDatatypeStylesheet;
029: import com.metaboss.sdlctools.services.codegenerationstylesheet.STDocumentationStylesheet;
030: import com.metaboss.util.StringUtils;
031:
032: /** The basic string datatype. */
033: public final class StringField implements JavaTemplate {
034: // This class keeps the category metadata
035: private class CategoryMetadata {
036: public String Name;
037: public String Description;
038: public String PartOfIdentifierName;
039: public String ValuePattern;
040: }
041:
042: /* This method is called to perform the template merge with the values supplied in context */
043: public void mergeTemplate(PrintWriter pOutputWriter,
044: JavaTemplateContext pContext) throws JavaTemplateException {
045: DataType lDatatype = (DataType) pContext
046: .getMandatoryObject("DataType");
047: STDocumentationStylesheet lDocumentationStylesheet = null;
048: STDatatypeStylesheet lDataTypeStylesheet = null;
049: try {
050: lDocumentationStylesheet = CodeGenerationStylesheetAccessor
051: .getDocumentationStylesheet();
052: lDataTypeStylesheet = CodeGenerationStylesheetAccessor
053: .getDatatypeStylesheet(lDatatype);
054: } catch (BSException e) {
055: throw new JavaTemplateException(
056: "Unable to obtain Datatype stylesheet for "
057: + lDatatype.getRef(), e);
058: }
059: String lDatatypeName = lDataTypeStylesheet.getNormalisedName();
060: Properties lTypetemplateProperties = lDataTypeStylesheet
061: .getTypetemplateProperties();
062:
063: int lMaxLength = Integer.parseInt(lTypetemplateProperties
064: .getProperty("MaxLength", "100"));
065: int lMinLength = Integer.parseInt(lTypetemplateProperties
066: .getProperty("MinLength", "0"));
067: String lEmptyString = lTypetemplateProperties.getProperty(
068: "EmptyString", "");
069: String lConcealedString = lTypetemplateProperties.getProperty(
070: "ConcealedString", "xxxxxx");
071: boolean lComparable = Boolean.valueOf(
072: lTypetemplateProperties.getProperty("Comparable",
073: "true")).booleanValue();
074: String lValidPattern = lTypetemplateProperties.getProperty(
075: "ValidPattern", null);
076: // Work with categories
077: CategoryMetadata[] lCategories = new CategoryMetadata[0];
078: {
079: HashSet lUsedNames = new HashSet();
080: ArrayList lCategoriesArray = new ArrayList();
081: for (int i = 1;; i++) {
082: String lName = lTypetemplateProperties.getProperty(
083: "Category[" + i + "].Name", null);
084: if (lName == null)
085: break; //no more names
086: if (!lUsedNames.add(lName))
087: throw new JavaTemplateException(
088: "Property Category["
089: + i
090: + "].Name contains duplicate value '"
091: + lName + "'.");
092: CategoryMetadata lMetadata = new CategoryMetadata();
093: lMetadata.Name = lName;
094: lMetadata.Description = lTypetemplateProperties
095: .getProperty("Category[" + i + "].Description",
096: null);
097: if (lMetadata.Description == null
098: || lMetadata.Description.trim().length() == 0)
099: lMetadata.Description = lDocumentationStylesheet
100: .getMissingDescriptionHtmlText();
101: lMetadata.PartOfIdentifierName = StringUtils
102: .suggestName(lName, true, false);
103: // Watch for clashes with isEmpty() and isConcealed() methods
104: if (lMetadata.PartOfIdentifierName.equals("Empty")
105: || lMetadata.PartOfIdentifierName
106: .equals("Concealed"))
107: throw new JavaTemplateException(
108: "Category name "
109: + lName
110: + " can not be used. This name is reserved for generated constant or method names.");
111: lMetadata.ValuePattern = lTypetemplateProperties
112: .getProperty(
113: "Category[" + i + "].ValuePattern",
114: null);
115: if (lMetadata.ValuePattern == null
116: || lMetadata.ValuePattern.length() == 0)
117: throw new JavaTemplateException(
118: "Property 'Category["
119: + i
120: + "].ValuePattern' is not found or is empty. This property must be defined.");
121: lCategoriesArray.add(lMetadata);
122: }
123: lCategories = (CategoryMetadata[]) lCategoriesArray
124: .toArray(new CategoryMetadata[lCategoriesArray
125: .size()]);
126: }
127:
128: pOutputWriter.println(lDocumentationStylesheet
129: .getCopyrightCommentInJava());
130: pOutputWriter.println("package "
131: + lDataTypeStylesheet.getPackageName() + ";");
132: pOutputWriter.println();
133: pOutputWriter.println("import java.io.Serializable;");
134: pOutputWriter
135: .println("import com.metaboss.enterprise.datatypes.DataType;");
136: pOutputWriter
137: .println("import com.metaboss.enterprise.datatypes.DataTypeTranslationMetadata;");
138: pOutputWriter
139: .println("import com.metaboss.enterprise.datatypes.DataTypeValidationException;");
140: pOutputWriter
141: .println("import com.metaboss.enterprise.datatypes.DefaultDataTypeTranslationMetadataImplementation;");
142: pOutputWriter
143: .println("import com.metaboss.enterprise.datatypes.DataTypeInstanceCreationFailureException;");
144: pOutputWriter
145: .println("import com.metaboss.enterprise.datatypes.DataTypeInvalidOperationForEmptyInstanceException;");
146: pOutputWriter
147: .println("import com.metaboss.enterprise.datatypes.DataTypeInvalidOperationForConcealedInstanceException;");
148: pOutputWriter
149: .println("import com.metaboss.enterprise.datatypes.sqltranslation.SqlVarcharTranslator;");
150: pOutputWriter
151: .println("import com.metaboss.enterprise.datatypes.primitivetranslation.PrimitiveStringTranslator;");
152: pOutputWriter
153: .println("import com.metaboss.enterprise.datatypes.xmltranslation.XmlTextTranslator;");
154:
155: String lDescriptionHtmlText = lDataTypeStylesheet
156: .getDescriptionHtmlText();
157: pOutputWriter.println("/** " + lDescriptionHtmlText
158: + (lDescriptionHtmlText.endsWith(".") ? "" : "."));
159: pOutputWriter
160: .println(" * <p>This datatype is a string type, which means that it contains a string value inside.");
161: pOutputWriter
162: .println(" * It has following characteristics:</p>");
163: pOutputWriter.println(" * <p><table border=\"0\">");
164: pOutputWriter
165: .println(" * <tr><td>Empty Value:</td><td> </td><td>'"
166: + lEmptyString + "'</td></tr>");
167: pOutputWriter
168: .println(" * <tr><td>Concealed Value:</td><td> </td><td>'"
169: + lConcealedString + "'</td></tr>");
170: pOutputWriter
171: .println(" * <tr><td>Is Comparable:</td><td> </td><td>"
172: + (lComparable ? "yes" : "no") + "</td></tr>");
173: pOutputWriter
174: .println(" * <tr><td>Minimum Length (characters):</td><td> </td><td>"
175: + Integer.toString(lMinLength) + "</td></tr>");
176: pOutputWriter
177: .println(" * <tr><td>Maximum Length (characters):</td><td> </td><td>"
178: + Integer.toString(lMaxLength) + "</td></tr>");
179: pOutputWriter
180: .println(" * <tr><td>Pattern restriction (reg ex):</td><td> </td><td>"
181: + (lValidPattern != null ? lValidPattern
182: : "<i>none</i>") + "</td></tr>");
183: pOutputWriter.println(" * </table></p>");
184: if (lCategories.length > 0) {
185: pOutputWriter
186: .println(" * <p><b>Value categories are:</b>");
187: pOutputWriter.println(" * <table border=\"1\">");
188: pOutputWriter
189: .println(" * <tr><th>Category Name</th><th>Testing Method</th><th>Category Description</th><th>Category Pattern (reg ex)</th></tr>");
190: for (int i = 0; i < lCategories.length; i++) {
191: CategoryMetadata lCategoryMetadata = lCategories[i];
192: pOutputWriter
193: .println(" * <tr><td>"
194: + lCategoryMetadata.Name
195: + "</td><td><a href=\"#is"
196: + lCategoryMetadata.PartOfIdentifierName
197: + "()\">is"
198: + lCategoryMetadata.PartOfIdentifierName
199: + "()</a></td><td>"
200: + lCategoryMetadata.Description
201: + "</td><td>"
202: + lCategoryMetadata.ValuePattern
203: + "</td></tr>");
204: }
205: pOutputWriter.println(" * </table>");
206: }
207: pOutputWriter.println(" * </p>");
208: pOutputWriter.println(" */");
209: if (lComparable) {
210: pOutputWriter.println("public final class " + lDatatypeName
211: + " implements DataType, Serializable, Comparable");
212: } else {
213: pOutputWriter.println("public final class " + lDatatypeName
214: + " implements DataType, Serializable");
215: }
216: pOutputWriter.println("{");
217: pOutputWriter
218: .println(" // Static storage for translation metadata. Create once and return many times");
219: pOutputWriter
220: .println(" private static DataTypeTranslationMetadata sTranslationMetadata = null;");
221: pOutputWriter
222: .println(" // Static storage for the empty string constant.");
223: pOutputWriter
224: .println(" private static final String sEmptyString = \""
225: + lEmptyString + "\";");
226: pOutputWriter
227: .println(" // Static storage for the concealed string constant.");
228: pOutputWriter
229: .println(" private static final String sConcealedString = \""
230: + lConcealedString + "\";");
231: // Create valid pattern only once per JVM
232: if (lValidPattern != null) {
233: pOutputWriter
234: .println(" // Static precompiled pattern to use when validating strings.");
235: pOutputWriter
236: .println(" private static final java.util.regex.Pattern sValidPattern = java.util.regex.Pattern.compile(\""
237: + StringUtils
238: .suggestJavaStringConstant(lValidPattern)
239: + "\");");
240: }
241: // Generate methods for custom categories
242: for (int i = 0; i < lCategories.length; i++) {
243: CategoryMetadata lCategoryMetadata = lCategories[i];
244: pOutputWriter
245: .println(" // Static precompiled pattern to use when testing for the '"
246: + lCategoryMetadata.Name + "' category");
247: pOutputWriter
248: .println(" private static final java.util.regex.Pattern s"
249: + lCategoryMetadata.PartOfIdentifierName
250: + "Pattern = java.util.regex.Pattern.compile(\""
251: + lCategoryMetadata.ValuePattern + "\");");
252: }
253:
254: pOutputWriter
255: .println(" // State is stored here : 0 - normal, 1 - empty, 2 - concealed");
256: pOutputWriter.println(" private byte mState = 0;");
257: pOutputWriter.println(" // Actual value stored here");
258: pOutputWriter.println(" private String mValue;");
259:
260: pOutputWriter.println(" static");
261: pOutputWriter.println(" {");
262: pOutputWriter
263: .println(" SqlVarcharTranslator lSqlTranslator = new SqlVarcharTranslator()");
264: pOutputWriter.println(" {");
265: pOutputWriter
266: .println(" public String getSqlValue(DataType pDataTypeInstance) throws ClassCastException, DataTypeInvalidOperationForEmptyInstanceException, DataTypeInvalidOperationForConcealedInstanceException");
267: pOutputWriter.println(" {");
268: pOutputWriter.println(" " + lDatatypeName
269: + " lInstance = (" + lDatatypeName
270: + ")pDataTypeInstance;");
271: pOutputWriter
272: .println(" if (lInstance.isConcealed())");
273: pOutputWriter
274: .println(" throw new DataTypeInvalidOperationForConcealedInstanceException();");
275: pOutputWriter
276: .println(" if (lInstance.isEmpty())");
277: pOutputWriter
278: .println(" throw new DataTypeInvalidOperationForEmptyInstanceException();");
279: pOutputWriter
280: .println(" return lInstance.mValue;");
281: pOutputWriter.println(" }");
282: pOutputWriter
283: .println(" public DataType createFromSqlValue(String pSqlValue) throws DataTypeInstanceCreationFailureException");
284: pOutputWriter.println(" {");
285: pOutputWriter.println(" if (pSqlValue == null)");
286: pOutputWriter
287: .println(" throw new DataTypeInstanceCreationFailureException(\"Supplied Sql value is null.\");");
288: pOutputWriter.println(" " + lDatatypeName
289: + " lValue = new " + lDatatypeName + "();");
290: pOutputWriter
291: .println(" lValue.mValue = pSqlValue;");
292: pOutputWriter.println(" return lValue;");
293: pOutputWriter.println(" }");
294: pOutputWriter.println(" };");
295: pOutputWriter
296: .println(" PrimitiveStringTranslator lPrimitiveTranslator = new PrimitiveStringTranslator()");
297: pOutputWriter.println(" {");
298: pOutputWriter
299: .println(" public String getPrimitiveValue(DataType pDataTypeInstance) throws ClassCastException, DataTypeInvalidOperationForEmptyInstanceException, DataTypeInvalidOperationForConcealedInstanceException");
300: pOutputWriter.println(" {");
301: pOutputWriter.println(" " + lDatatypeName
302: + " lInstance = (" + lDatatypeName
303: + ")pDataTypeInstance;");
304: pOutputWriter
305: .println(" if (lInstance.isConcealed())");
306: pOutputWriter
307: .println(" throw new DataTypeInvalidOperationForConcealedInstanceException();");
308: pOutputWriter
309: .println(" if (lInstance.isEmpty())");
310: pOutputWriter
311: .println(" throw new DataTypeInvalidOperationForEmptyInstanceException();");
312: pOutputWriter
313: .println(" return lInstance.mValue;");
314: pOutputWriter.println(" }");
315: pOutputWriter
316: .println(" public DataType createFromPrimitiveValue(String pPrimitiveValue) throws DataTypeInstanceCreationFailureException");
317: pOutputWriter.println(" {");
318: pOutputWriter
319: .println(" if (pPrimitiveValue == null)");
320: pOutputWriter
321: .println(" throw new DataTypeInstanceCreationFailureException(\"Supplied Primitive value is null.\");");
322: pOutputWriter.println(" " + lDatatypeName
323: + " lValue = new " + lDatatypeName + "();");
324: pOutputWriter
325: .println(" lValue.mValue = pPrimitiveValue;");
326: pOutputWriter.println(" return lValue;");
327: pOutputWriter.println(" }");
328: pOutputWriter.println(" };");
329: pOutputWriter
330: .println(" XmlTextTranslator lXmlTranslator = new XmlTextTranslator()");
331: pOutputWriter.println(" {");
332: pOutputWriter
333: .println(" public String getXmlValue(DataType pDataTypeInstance) throws ClassCastException, DataTypeInvalidOperationForEmptyInstanceException, DataTypeInvalidOperationForConcealedInstanceException");
334: pOutputWriter.println(" {");
335: pOutputWriter.println(" " + lDatatypeName
336: + " lInstance = (" + lDatatypeName
337: + ")pDataTypeInstance;");
338: pOutputWriter
339: .println(" if (lInstance.isConcealed())");
340: pOutputWriter
341: .println(" throw new DataTypeInvalidOperationForConcealedInstanceException();");
342: pOutputWriter
343: .println(" if (lInstance.isEmpty())");
344: pOutputWriter
345: .println(" throw new DataTypeInvalidOperationForEmptyInstanceException();");
346: pOutputWriter
347: .println(" return lInstance.mValue;");
348: pOutputWriter.println(" }");
349: pOutputWriter
350: .println(" public DataType createFromXmlValue(String pXmlValue) throws DataTypeInstanceCreationFailureException");
351: pOutputWriter.println(" {");
352: pOutputWriter.println(" if (pXmlValue == null)");
353: pOutputWriter
354: .println(" throw new DataTypeInstanceCreationFailureException(\"Supplied Xml value is null.\");");
355: pOutputWriter.println(" " + lDatatypeName
356: + " lValue = new " + lDatatypeName + "();");
357: pOutputWriter
358: .println(" lValue.mValue = pXmlValue;");
359: pOutputWriter.println(" return lValue;");
360: pOutputWriter.println(" }");
361: pOutputWriter.println(" };");
362: pOutputWriter
363: .println(" java.util.Properties lProperties = new java.util.Properties();");
364: if (lMinLength > 0)
365: pOutputWriter
366: .println(" lProperties.setProperty(\"minsize\",\""
367: + lMinLength + "\");");
368: pOutputWriter
369: .println(" lProperties.setProperty(\"maxsize\",\""
370: + lMaxLength + "\");");
371: pOutputWriter
372: .println(" sTranslationMetadata = new DefaultDataTypeTranslationMetadataImplementation(DataTypeTranslationMetadata.SQL_VARCHAR, lProperties, lSqlTranslator, DataTypeTranslationMetadata.PRIMITIVE_STRING, null, lPrimitiveTranslator, DataTypeTranslationMetadata.XML_TEXT, null, lXmlTranslator);");
373: pOutputWriter.println(" }");
374:
375: pOutputWriter
376: .println(" /** Returns this datatype's translation metadata.");
377: pOutputWriter
378: .println(" * See {@link com.metaboss.enterprise.datatypes.DataType DataType} for more details */");
379: pOutputWriter
380: .println(" public static DataTypeTranslationMetadata getTranslationMetadata()");
381: pOutputWriter.println(" {");
382: pOutputWriter.println(" return sTranslationMetadata;");
383: pOutputWriter.println(" }");
384:
385: pOutputWriter
386: .println(" /** Returns string used to represent empty value in a toString() / createFromString() methods */");
387: pOutputWriter
388: .println(" public static String getEmptyString()");
389: pOutputWriter.println(" {");
390: pOutputWriter.println(" return sEmptyString;");
391: pOutputWriter.println(" }");
392:
393: pOutputWriter
394: .println(" /** Returns string used to represent concealed value in a toString() / createFromString() methods */");
395: pOutputWriter
396: .println(" public static String getConcealedString()");
397: pOutputWriter.println(" {");
398: pOutputWriter.println(" return sConcealedString;");
399: pOutputWriter.println(" }");
400:
401: pOutputWriter
402: .println(" /** Returns maximum length of string this datatype will take or return in toString() / createFromString() methods */");
403: pOutputWriter
404: .println(" public static long getMaxStringLength()");
405: pOutputWriter.println(" {");
406: pOutputWriter.println(" return "
407: + Integer.toString(lMaxLength) + "L;");
408: pOutputWriter.println(" }");
409:
410: pOutputWriter
411: .println(" /** Returns minimum length of string this datatype will take or return in toString() / createFromString() methods */");
412: pOutputWriter
413: .println(" public static long getMinStringLength()");
414: pOutputWriter.println(" {");
415: pOutputWriter.println(" return "
416: + Integer.toString(lMinLength) + "L;");
417: pOutputWriter.println(" }");
418:
419: pOutputWriter
420: .println(" /** Creates an instance of the datatype carrying an empty value */");
421: pOutputWriter.println(" public static " + lDatatypeName
422: + " createEmpty()");
423: pOutputWriter.println(" {");
424: pOutputWriter.println(" " + lDatatypeName
425: + " lValue = new " + lDatatypeName + "();");
426: pOutputWriter.println(" lValue.mState = 1;");
427: pOutputWriter.println(" return lValue;");
428: pOutputWriter.println(" }");
429:
430: pOutputWriter
431: .println(" /** Creates an instance of the datatype carrying concealed value */");
432: pOutputWriter.println(" public static " + lDatatypeName
433: + " createConcealed()");
434: pOutputWriter.println(" {");
435: pOutputWriter.println(" " + lDatatypeName
436: + " lValue = new " + lDatatypeName + "();");
437: pOutputWriter.println(" lValue.mState = 2;");
438: pOutputWriter.println(" return lValue;");
439: pOutputWriter.println(" }");
440:
441: pOutputWriter
442: .println(" /** Creates an instance of the datatype from given readable value */");
443: pOutputWriter
444: .println(" public static "
445: + lDatatypeName
446: + " createFromString( String pSourceString) throws DataTypeValidationException");
447: pOutputWriter.println(" {");
448: pOutputWriter.println(" // Perform validation");
449: pOutputWriter.println(" validateString(pSourceString);");
450: pOutputWriter.println(" // Create instance");
451: pOutputWriter
452: .println(" if (pSourceString.equals(sEmptyString))");
453: pOutputWriter.println(" return createEmpty();");
454: pOutputWriter
455: .println(" if (pSourceString.equals(sConcealedString))");
456: pOutputWriter.println(" return createConcealed();");
457: pOutputWriter.println(" // Create instance");
458: pOutputWriter.println(" " + lDatatypeName
459: + " lValue = new " + lDatatypeName + "();");
460: pOutputWriter.println(" lValue.mValue = pSourceString;");
461: pOutputWriter.println(" return lValue;");
462: pOutputWriter.println(" }");
463:
464: pOutputWriter
465: .println(" /** Validates proposed readable value */");
466: pOutputWriter
467: .println(" public static void validateString( String pProposedString) throws DataTypeValidationException");
468: pOutputWriter.println(" {");
469: pOutputWriter.println(" if (pProposedString == null)");
470: pOutputWriter
471: .println(" throw new DataTypeValidationException(\"Null initialisation string is not allowed for "
472: + lDatatypeName
473: + ". Use createEmpty() method to create an empty "
474: + lDatatypeName + "\");");
475: pOutputWriter
476: .println(" if (pProposedString.equals(sEmptyString))");
477: pOutputWriter.println(" return; // Done validation");
478: pOutputWriter
479: .println(" if (pProposedString.equals(sConcealedString))");
480: pOutputWriter
481: .println(" return; // Done validation");
482: pOutputWriter
483: .println(" int lLength = pProposedString.length();");
484: pOutputWriter.println(" if (lLength > " + lMaxLength
485: + ")");
486: pOutputWriter
487: .println(" throw new DataTypeValidationException(\"String is too long for "
488: + lDatatypeName
489: + ". Expected max length is "
490: + lMaxLength + " characters.\");");
491: if (lMinLength > 0) {
492: pOutputWriter.println(" if (lLength < " + lMinLength
493: + ")");
494: pOutputWriter
495: .println(" throw new DataTypeValidationException(\"String is too short for "
496: + lDatatypeName
497: + ". Expected min length is "
498: + lMinLength
499: + " characters.\");");
500: }
501: if (lValidPattern != null) {
502: pOutputWriter
503: .println(" if (!sValidPattern.matcher(pProposedString).matches())");
504: pOutputWriter
505: .println(" throw new DataTypeValidationException(\"String '\" + pProposedString + \"' does not appear to be valid because it does not match the required pattern '\" + sValidPattern.pattern() + \"'.\");");
506: }
507: pOutputWriter.println(" }");
508:
509: if (lComparable) {
510: pOutputWriter
511: .println(" /** Compares this instance to another");
512: pOutputWriter
513: .println(" * Two empty instances considered equal. The empty instance is always less than any non-empty one.");
514: pOutputWriter
515: .println(" * Two concealed instances considered equal. The concealed instance is always less than any non-concealed one.");
516: pOutputWriter
517: .println(" * @return a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.");
518: pOutputWriter
519: .println(" * @exception java.lang.ClassCastException if the specified object's type prevents it from being compared to this Object */");
520: pOutputWriter
521: .println(" public int compareTo(Object pOtherObject)");
522: pOutputWriter.println(" {");
523: pOutputWriter.println(" " + lDatatypeName
524: + " lOtherInstance = (" + lDatatypeName
525: + ")pOtherObject;");
526: pOutputWriter
527: .println(" if (lOtherInstance.isEmpty() && isEmpty())");
528: pOutputWriter
529: .println(" return 0; // Both instances are empty and therefore equal.");
530: pOutputWriter.println(" if (isEmpty())");
531: pOutputWriter
532: .println(" return -1; // Anything bigger than empty");
533: pOutputWriter
534: .println(" if (lOtherInstance.isEmpty())");
535: pOutputWriter
536: .println(" return 1; // Anything bigger than empty");
537: pOutputWriter
538: .println(" if (lOtherInstance.isConcealed() && isConcealed())");
539: pOutputWriter
540: .println(" return 0; // Both instances are concealed and therefore equal.");
541: pOutputWriter.println(" if (isConcealed())");
542: pOutputWriter
543: .println(" return -1; // Anything bigger than concealed");
544: pOutputWriter
545: .println(" if (lOtherInstance.isConcealed())");
546: pOutputWriter
547: .println(" return 1; // Anything bigger than concealed");
548: pOutputWriter
549: .println(" return mValue.compareTo(lOtherInstance.mValue);");
550: pOutputWriter.println(" }");
551: }
552: pOutputWriter
553: .println(" /** Returns the value inside this instance of ${DataType.getName()} as readable string.");
554: pOutputWriter
555: .println(" * Produced string is always the same as in getAsString() method. */");
556: pOutputWriter.println(" public String toString()");
557: pOutputWriter.println(" {");
558: pOutputWriter.println(" return getAsString();");
559: pOutputWriter.println(" }");
560:
561: pOutputWriter
562: .println(" /** Returns the value inside this instance of "
563: + lDatatypeName + " as readable string */");
564: pOutputWriter.println(" public String getAsString()");
565: pOutputWriter.println(" {");
566: pOutputWriter.println(" if (isEmpty())");
567: pOutputWriter.println(" return sEmptyString;");
568: pOutputWriter.println(" if (isConcealed())");
569: pOutputWriter.println(" return sConcealedString;");
570: pOutputWriter.println(" return mValue;");
571: pOutputWriter.println(" }");
572:
573: pOutputWriter
574: .println(" /** Indicates whether some other object is \"equal to\" this one");
575: pOutputWriter
576: .println(" * @return true if this object is the same as the obj argument; false otherwise. */");
577: pOutputWriter
578: .println(" public boolean equals(Object pOtherObject)");
579: pOutputWriter.println(" {");
580: pOutputWriter.println(" if (pOtherObject == null)");
581: pOutputWriter.println(" return false;");
582: pOutputWriter.println(" if ((pOtherObject instanceof "
583: + lDatatypeName + ") == false)");
584: pOutputWriter.println(" return false;");
585: pOutputWriter.println(" if (((" + lDatatypeName
586: + ")pOtherObject).isConcealed() || isConcealed())");
587: pOutputWriter
588: .println(" return false; // One of the instances is concealed");
589: pOutputWriter.println(" if (((" + lDatatypeName
590: + ")pOtherObject).isEmpty() && isEmpty())");
591: pOutputWriter
592: .println(" return true; // Both instances are empty.");
593: pOutputWriter.println(" if (((" + lDatatypeName
594: + ")pOtherObject).isEmpty() != isEmpty())");
595: pOutputWriter
596: .println(" return false; // One of the instances is empty, the other one is not");
597: pOutputWriter.println(" return ((" + lDatatypeName
598: + ")pOtherObject).mValue.equals(mValue);");
599: pOutputWriter.println(" }");
600:
601: pOutputWriter
602: .println(" /** Returns a hash code value for the object. This method is supported for the benefit of hashtables");
603: pOutputWriter
604: .println(" * @return instance's hash code or zero if this instance is empty or concealed */");
605: pOutputWriter.println(" public int hashCode()");
606: pOutputWriter.println(" {");
607: pOutputWriter
608: .println(" if (isEmpty() || isConcealed())");
609: pOutputWriter.println(" return 0;");
610: pOutputWriter.println(" return mValue.hashCode();");
611: pOutputWriter.println(" }");
612:
613: pOutputWriter
614: .println(" /** Returns true if the instance contains an empty value */");
615: pOutputWriter.println(" public boolean isEmpty()");
616: pOutputWriter.println(" {");
617: pOutputWriter.println(" return mState == 1;");
618: pOutputWriter.println(" }");
619:
620: pOutputWriter
621: .println(" /** Returns true if the instance contains concealed value */");
622: pOutputWriter.println(" public boolean isConcealed()");
623: pOutputWriter.println(" {");
624: pOutputWriter.println(" return mState == 2;");
625: pOutputWriter.println(" }");
626:
627: // Generate methods for custom categories
628: for (int i = 0; i < lCategories.length; i++) {
629: CategoryMetadata lCategoryMetadata = lCategories[i];
630: pOutputWriter
631: .println(" /** Returns true if the instance belongs to the '"
632: + lCategoryMetadata.Name + "' category.");
633: pOutputWriter
634: .println(" * Category description is : <i> "
635: + lCategoryMetadata.Description
636: + " </i> */");
637: pOutputWriter.println(" public boolean is"
638: + lCategoryMetadata.PartOfIdentifierName + "()");
639: pOutputWriter.println(" {");
640: pOutputWriter
641: .println(" if (isEmpty() || isConcealed())");
642: pOutputWriter.println(" return false;");
643: pOutputWriter.println(" return s"
644: + lCategoryMetadata.PartOfIdentifierName
645: + "Pattern.matcher(mValue).matches();");
646: pOutputWriter.println(" }");
647: }
648:
649: pOutputWriter
650: .println(" // Private access. External callers must use one of the static creators");
651: pOutputWriter.println(" private " + lDatatypeName + "()");
652: pOutputWriter.println(" {");
653: pOutputWriter.println(" }");
654: pOutputWriter.println("}");
655: }
656: }
|