r811 - trunk/eugene/src/main/java/org/nuiton/eugene/java
Author: tchemit Date: 2010-01-29 21:09:16 +0100 (Fri, 29 Jan 2010) New Revision: 811 Added: trunk/eugene/src/main/java/org/nuiton/eugene/java/ConstantsManager.java trunk/eugene/src/main/java/org/nuiton/eugene/java/ConstantsManagerExtension.java Modified: trunk/eugene/src/main/java/org/nuiton/eugene/java/JavaBuilder.java trunk/eugene/src/main/java/org/nuiton/eugene/java/ObjectModelTransformerToJava.java Log: - Evolution #296: Add a ConstantsManagerExtension to ObjectModel - reformat code (80 caracters maximum for a line) Added: trunk/eugene/src/main/java/org/nuiton/eugene/java/ConstantsManager.java =================================================================== --- trunk/eugene/src/main/java/org/nuiton/eugene/java/ConstantsManager.java (rev 0) +++ trunk/eugene/src/main/java/org/nuiton/eugene/java/ConstantsManager.java 2010-01-29 20:09:16 UTC (rev 811) @@ -0,0 +1,65 @@ +/* + * *##% + * EUGene :: EUGene + * Copyright (C) 2004 - 2010 CodeLutin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * ##%* + */ +package org.nuiton.eugene.java; + +import org.nuiton.eugene.GeneratorUtil; + +import java.util.Map; +import java.util.TreeMap; + +/** + * Manager of constant names. + * + * @author tchemit < chemit@codelutin.com > + * @since 2.3 + */ +public class ConstantsManager { + /** + * cache of constant name (values) for property name (keys) + */ + protected Map<String, String> nameToConstant; + + /** + * Obtain a constant nmae from a property name and store it in cache + * the first time it had to build it. + * + * @param propertyName the propertyName to convert + * @return the equivalent constant name + */ + public String getConstantName(String propertyName) { + Map<String, String> map = getNameToConstant(); + if (map.containsKey(propertyName)) { + return map.get(propertyName); + } + // convert propertyName to constant name + String constantName = + GeneratorUtil.convertVariableNameToConstantName(propertyName); + map.put(propertyName,constantName); + return constantName; + } + + protected Map<String, String> getNameToConstant() { + if (nameToConstant == null) { + nameToConstant = new TreeMap<String, String>(); + } + return nameToConstant; + } +} \ No newline at end of file Property changes on: trunk/eugene/src/main/java/org/nuiton/eugene/java/ConstantsManager.java ___________________________________________________________________ Added: svn:keywords + "Author Date Id Revision HeadURL Added: trunk/eugene/src/main/java/org/nuiton/eugene/java/ConstantsManagerExtension.java =================================================================== --- trunk/eugene/src/main/java/org/nuiton/eugene/java/ConstantsManagerExtension.java (rev 0) +++ trunk/eugene/src/main/java/org/nuiton/eugene/java/ConstantsManagerExtension.java 2010-01-29 20:09:16 UTC (rev 811) @@ -0,0 +1,50 @@ +/* + * *##% + * EUGene :: EUGene + * Copyright (C) 2004 - 2010 CodeLutin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * ##%* + */ +package org.nuiton.eugene.java; + +/** + * @author tchemit < chemit@codelutin.com > + * @since 2.3 + */ +public class ConstantsManagerExtension { + + /** + * Extension static used to identify {@link ConstantsManagerExtension] in + * ObjectModel + */ + public static final String OBJECTMODEL_EXTENSION = "constants"; + + /** + * the unique manager to use + */ + protected ConstantsManager manager = new ConstantsManager(); + + /** + * Obtain a constant nmae from a property name and store it in cache + * the first time it had to build it. + * + * @param propertyName the propertyName to convert + * @return the equivalent constant name + */ + public String getConstantName(String propertyName) { + return manager.getConstantName(propertyName); + } +} \ No newline at end of file Property changes on: trunk/eugene/src/main/java/org/nuiton/eugene/java/ConstantsManagerExtension.java ___________________________________________________________________ Added: svn:keywords + "Author Date Id Revision HeadURL Modified: trunk/eugene/src/main/java/org/nuiton/eugene/java/JavaBuilder.java =================================================================== --- trunk/eugene/src/main/java/org/nuiton/eugene/java/JavaBuilder.java 2010-01-29 20:08:21 UTC (rev 810) +++ trunk/eugene/src/main/java/org/nuiton/eugene/java/JavaBuilder.java 2010-01-29 20:09:16 UTC (rev 811) @@ -63,15 +63,26 @@ * ObjectModel extension to manage imports : one ImportsManager by classifier */ protected AnnotationsManagerExtension annotationsManagerExtension; + /** + * ObjectModel extension to manage constants : one ConstantsManager for all + */ + protected ConstantsManagerExtension constanssManagerExtension; @SuppressWarnings("unchecked") public JavaBuilder(String modelName) { this.modelBuilder = new ObjectModelBuilder(modelName); this.importsManagerExtension = getModel().getExtension( - ImportsManagerExtension.OBJECTMODEL_EXTENSION, ImportsManagerExtension.class); + ImportsManagerExtension.OBJECTMODEL_EXTENSION, + ImportsManagerExtension.class); + this.annotationsManagerExtension = getModel().getExtension( - AnnotationsManagerExtension.OBJECTMODEL_EXTENSION, AnnotationsManagerExtension.class); + AnnotationsManagerExtension.OBJECTMODEL_EXTENSION, + AnnotationsManagerExtension.class); + + this.constanssManagerExtension = getModel().getExtension( + ConstantsManagerExtension.OBJECTMODEL_EXTENSION, + ConstantsManagerExtension.class); } /** @@ -83,7 +94,8 @@ return this.modelBuilder.getModel(); } - public void setDocumentation(ObjectModelElement element, String documentation) { + public void setDocumentation(ObjectModelElement element, + String documentation) { modelBuilder.setDocumentation(element, documentation); } @@ -115,7 +127,8 @@ for (String oneType : GeneratorUtil.getTypesList(imports)) { manager.addImport(oneType); if (log.isDebugEnabled()) { - log.debug("Add import for : " + classifier.getQualifiedName() + " _ import: " + oneType); + log.debug("Add import for : " + classifier.getQualifiedName() + + " _ import: " + oneType); } } } @@ -127,19 +140,32 @@ * @param element element on which add annotation * @param annotation annotation to add */ - public void addAnnotation(ObjectModelClassifier classifier, ObjectModelElement element, String annotation) { + public void addAnnotation(ObjectModelClassifier classifier, + ObjectModelElement element, + String annotation) { if (annotation == null) { return; } - AnnotationsManager manager = annotationsManagerExtension.getManager(classifier); + AnnotationsManager manager = + annotationsManagerExtension.getManager(classifier); annotation = annotation.trim(); manager.addAnnotation(element, annotation); if (log.isDebugEnabled()) { - log.debug("Add annotation for <" + classifier.getQualifiedName() + ":" + element.getName() + "> : " + annotation); + log.debug("Add annotation for <" + classifier.getQualifiedName() + + ":" + element.getName() + "> : " + annotation); } } + public String getConstantName(String propertyName) { + String result = constanssManagerExtension.getConstantName(propertyName); + if (log.isDebugEnabled()) { + log.debug("get constant name for <" + propertyName + "> : " + + result); + } + return result; + } + /** * Create a new class in the model. * @@ -160,8 +186,10 @@ * @return a new ObjectModelClass * @see org.nuiton.eugene.models.object.ObjectModelBuilder#createClass(java.lang.String,java.lang.String, org.nuiton.eugene.models.object.ObjectModelModifier...) */ - public ObjectModelClass createAbstractClass(String name, String packageName) { - return modelBuilder.createClass(name, packageName, ObjectModelModifier.ABSTRACT); + public ObjectModelClass createAbstractClass(String name, + String packageName) { + return modelBuilder.createClass(name, packageName, + ObjectModelModifier.ABSTRACT); } /** @@ -172,7 +200,8 @@ * @return a new ObjectModelInterface * @see org.nuiton.eugene.models.object.ObjectModelBuilder#createInterface(java.lang.String, java.lang.String) */ - public ObjectModelInterface createInterface(String name, String packageName) { + public ObjectModelInterface createInterface(String name, + String packageName) { return modelBuilder.createInterface(name, packageName); } @@ -183,7 +212,8 @@ * @param packageName package's name of the enumeration to create * @return a new ObjectModelEnumeration */ - public ObjectModelEnumeration createEnumeration(String name, String packageName) { + public ObjectModelEnumeration createEnumeration(String name, + String packageName) { return modelBuilder.createEnumeration(name, packageName); } @@ -196,7 +226,8 @@ * @see org.nuiton.eugene.models.object.ObjectModelBuilder#addInterface( *org.nuiton.eugene.models.object.ObjectModelClassifier, java.lang.String) */ - public void setSuperClass(ObjectModelClass classifier, String superclassQualifiedName) { + public void setSuperClass(ObjectModelClass classifier, + String superclassQualifiedName) { ObjectModelClassImpl impl = (ObjectModelClassImpl) classifier; impl.clearSuperclasses(); // suppress all existing superclass: only one can be set for java modelBuilder.addSuperclass(impl, superclassQualifiedName); @@ -212,7 +243,8 @@ * @see org.nuiton.eugene.models.object.ObjectModelBuilder#addInterface( *org.nuiton.eugene.models.object.ObjectModelClassifier, java.lang.String) */ - public void addInterface(ObjectModelClassifier classifier, String interfaceQualifiedName) { + public void addInterface(ObjectModelClassifier classifier, + String interfaceQualifiedName) { modelBuilder.addInterface(classifier, interfaceQualifiedName); this.addImport(classifier, interfaceQualifiedName); } @@ -229,11 +261,15 @@ * @return a new ObjectModelAttribute * @see org.nuiton.eugene.models.object.ObjectModelBuilder#addAttribute(org.nuiton.eugene.models.object.ObjectModelClassifier, java.lang.String,java.lang.String, java.lang.String, org.nuiton.eugene.models.object.ObjectModelModifier...) */ - public ObjectModelAttribute addAttribute(ObjectModelClassifier classifier, String name, String type, String value, + public ObjectModelAttribute addAttribute(ObjectModelClassifier classifier, + String name, + String type, + String value, ObjectModelModifier... modifiers) { this.addImport(classifier, type); this.addImport(classifier, value); - return modelBuilder.addAttribute(classifier, name, type, value, modifiers); + return modelBuilder.addAttribute(classifier, name, type, value, + modifiers); } /** @@ -248,10 +284,15 @@ * @return a new ObjectModelAttribute * @throws IllegalArgumentException if the modifier is not a visibility */ - public ObjectModelAttribute addConstant(ObjectModelClassifier classifier, String name, String type, String value, - ObjectModelModifier visibility) throws IllegalArgumentException { + public ObjectModelAttribute addConstant(ObjectModelClassifier classifier, + String name, + String type, + String value, + ObjectModelModifier visibility) + throws IllegalArgumentException { if (!visibility.isVisibility()) { - throw new IllegalArgumentException("Illegal visibility type : " + visibility.name() + + throw new IllegalArgumentException("Illegal visibility type : " + + visibility.name() + " for " + classifier.getQualifiedName()); } @@ -268,8 +309,11 @@ * @param type * @return a new ObjectModelAttribute */ - public ObjectModelAttribute addAttribute(ObjectModelClassifier classifier, String name, String type) { - return addAttribute(classifier, name, type, "", ObjectModelModifier.PROTECTED); + public ObjectModelAttribute addAttribute(ObjectModelClassifier classifier, + String name, + String type) { + return addAttribute(classifier, name, type, "", + ObjectModelModifier.PROTECTED); } /** @@ -280,7 +324,8 @@ * @param attribute * @return a new ObjectModelAttribute */ - public ObjectModelAttribute addAttribute(ObjectModelClassifier classifier, ObjectModelAttribute attribute) { + public ObjectModelAttribute addAttribute(ObjectModelClassifier classifier, + ObjectModelAttribute attribute) { Set<ObjectModelModifier> modifiers = new HashSet<ObjectModelModifier>(); @@ -300,8 +345,11 @@ modifiers.add(ObjectModelModifier.PACKAGE); } - return addAttribute(classifier, attribute.getName(), attribute.getType(), - attribute.getDefaultValue(), modifiers.toArray(new ObjectModelModifier[modifiers.size()])); + return addAttribute(classifier, + attribute.getName(), + attribute.getType(), + attribute.getDefaultValue(), + modifiers.toArray(new ObjectModelModifier[modifiers.size()])); } /** @@ -314,7 +362,9 @@ * @return a new ObjectModelOperation * @see org.nuiton.eugene.models.object.ObjectModelBuilder#addOperation(org.nuiton.eugene.models.object.ObjectModelClassifier, java.lang.String,java.lang.String, org.nuiton.eugene.models.object.ObjectModelModifier...) */ - public ObjectModelOperation addOperation(ObjectModelClassifier classifier, String name, String type, + public ObjectModelOperation addOperation(ObjectModelClassifier classifier, + String name, + String type, ObjectModelModifier... modifiers) { this.addImport(classifier, type); return modelBuilder.addOperation(classifier, name, type, modifiers); @@ -330,7 +380,8 @@ */ public ObjectModelOperation addBlock(ObjectModelClassifier classifier, ObjectModelModifier... modifiers) { - ObjectModelOperationImpl operation = (ObjectModelOperationImpl) modelBuilder.addOperation(classifier, null, null, modifiers); + ObjectModelOperationImpl operation = (ObjectModelOperationImpl) + modelBuilder.addOperation(classifier, null, null, modifiers); if (Arrays.asList(modifiers).contains(ObjectModelModifier.STATIC)) { operation.setStatic(true); } @@ -345,10 +396,12 @@ * @return a new ObjectModelOperation * @throws IllegalArgumentException if the modifier is not a visibility */ - public ObjectModelOperation addConstructor(ObjectModelClass clazz, ObjectModelModifier visibility) + public ObjectModelOperation addConstructor(ObjectModelClass clazz, + ObjectModelModifier visibility) throws IllegalArgumentException { if (!visibility.isVisibility()) { - throw new IllegalArgumentException("Illegal visibility type : " + visibility.name() + + throw new IllegalArgumentException("Illegal visibility type : " + + visibility.name() + " for " + clazz.getQualifiedName()); } @@ -365,8 +418,11 @@ * @see org.nuiton.eugene.models.object.ObjectModelBuilder#addParameter( *org.nuiton.eugene.models.object.ObjectModelOperation, java.lang.String, java.lang.String) */ - public ObjectModelParameter addParameter(ObjectModelOperation operation, String type, String name) { - this.addImport((ObjectModelClassifier) operation.getDeclaringElement(), type); + public ObjectModelParameter addParameter(ObjectModelOperation operation, + String type, + String name) { + this.addImport((ObjectModelClassifier) operation.getDeclaringElement(), + type); return modelBuilder.addParameter(operation, type, name); } @@ -379,7 +435,8 @@ *org.nuiton.eugene.models.object.ObjectModelOperation, java.lang.String) */ public void addException(ObjectModelOperation operation, String exception) { - this.addImport((ObjectModelClassifier) operation.getDeclaringElement(), exception); + this.addImport((ObjectModelClassifier) operation.getDeclaringElement(), + exception); modelBuilder.addException(operation, exception); } @@ -395,19 +452,25 @@ public void setOperationBody(ObjectModelOperation operation, String body) throws IllegalArgumentException { if (operation.isAbstract()) { - throw new IllegalArgumentException("Unable to add body for an abstract method"); + throw new IllegalArgumentException("Unable to add body for an " + + "abstract method"); } modelBuilder.setOperationBody(operation, body); } - public ObjectModelClassifier addInnerClassifier(ObjectModelClass clazz, ObjectModelType type, String name) throws IllegalArgumentException { + public ObjectModelClassifier addInnerClassifier(ObjectModelClass clazz, + ObjectModelType type, + String name) + throws IllegalArgumentException { return modelBuilder.addInnerClassifier(clazz, type, name); } - public ObjectModelOperation addConstructor(ObjectModelEnumeration enumeration, ObjectModelModifier visibility) + public ObjectModelOperation addConstructor(ObjectModelEnumeration enumeration, + ObjectModelModifier visibility) throws IllegalArgumentException { if (!visibility.isVisibility()) { - throw new IllegalArgumentException("Illegal visibility type : " + visibility.name() + + throw new IllegalArgumentException("Illegal visibility type : " + + visibility.name() + " for " + enumeration.getQualifiedName()); } Modified: trunk/eugene/src/main/java/org/nuiton/eugene/java/ObjectModelTransformerToJava.java =================================================================== --- trunk/eugene/src/main/java/org/nuiton/eugene/java/ObjectModelTransformerToJava.java 2010-01-29 20:08:21 UTC (rev 810) +++ trunk/eugene/src/main/java/org/nuiton/eugene/java/ObjectModelTransformerToJava.java 2010-01-29 20:09:16 UTC (rev 811) @@ -123,6 +123,10 @@ builder.addImport(classifier, imports.getName()); } + public String getConstantName(String propertyName) { + return builder.getConstantName(propertyName); + } + protected ObjectModelAttribute addConstant(ObjectModelClassifier classifier, String name, String type, String value, ObjectModelModifier visibility) throws IllegalArgumentException { return builder.addConstant(classifier, name, type, value, visibility);
participants (1)
-
tchemit@users.nuiton.org