Ignore:
Timestamp:
06/28/13 11:56:03 (11 years ago)
Author:
psniegowski
Message:

HIGHLIGHTS:

  • FramsClass? and contained Param are now immutable classes (like String),

which allows to refer to them concurrently without synchronization
(which for example in turn simplifies GUI management)

  • also make Path immutable (which was earlier only assumed)
  • add global cache for FramsClasses? created solely and automatically

on base of Java classes.

representations basing on given FramsClass?

  • above changes greatly improved GUI responsivness during browsing
  • furtherly improve Param class hierarchy
  • allow to inject actions on state changes into MultiParamLoader?
  • add more tests

CHANGELOG:

Add StatusListener? to MultiParamLoader?.

Minor refactorization in MultiParamLoader?.

First step with auto append.

Add SchemaTest?.

Improve Registry.

Clean up in Registry.

Work out Registry.

Use annotations for Param.

Fix ListChange?.

Improve fluent interface of the FramsClassBuilder?.

Done caching of ReflectionAccess?.Backend

Fix hashCode of Pair.

A step on a way to cache ReflectionAccess?.Backend

Make SimpleAbstractAccess?.framsClass a final field.

Add static cache for FramsClasses? based on java.

Only classes created strictly and automatically
based on java classes are using this cache.

Make all Params immutable.

Many improvement to make Param immutable.

Make PrimitiveParam? generic type.

Several changes to make Param immutable.

Make FramsClass? immutable.

Another improvement to Path immutability.

Several improvements to Path.

Improve PathTest?.

Configurarable MutabilityDetector?.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • java/main/src/main/java/com/framsticks/params/ParamBuilder.java

    r86 r87  
    44import com.framsticks.params.annotations.ParamAnnotation;
    55import com.framsticks.params.types.*;
    6 import com.framsticks.util.lang.Numbers;
     6import com.framsticks.util.FramsticksException;
     7import com.framsticks.util.lang.Strings;
    78
    89import org.apache.log4j.Logger;
    910
    10 import java.util.ArrayList;
     11import java.lang.reflect.InvocationTargetException;
    1112import java.util.Arrays;
    12 
     13import java.util.List;
    1314
    1415/**
     
    5657        private Class<? extends Param> paramType;
    5758
    58         private Param param;
    59         private PrimitiveParam primitiveParam;
    60         private String typeString;
     59        private Object min;
     60
     61        private Object max;
     62
     63        private Object def;
     64
     65        String containedTypeName;
     66
     67        protected FramsClassBuilder classBuilder;
    6168
    6269        public ParamBuilder() {
     70                this(null);
     71        }
     72
     73
     74        /**
     75         * @param classBuilder
     76         */
     77        public ParamBuilder(FramsClassBuilder classBuilder) {
     78                this.classBuilder = classBuilder;
     79        }
     80
     81
     82        /**
     83         * @return the min
     84         */
     85        public Object getMin() {
     86                return min;
     87        }
     88
     89        /**
     90         * @return the max
     91         */
     92        public Object getMax() {
     93                return max;
     94        }
     95
     96        /**
     97         * @return the def
     98         */
     99        public Object getDef() {
     100                return def;
     101        }
     102
     103        public String getContainedTypeName() {
     104                return Strings.notEmpty(containedTypeName) ? containedTypeName : null;
     105        }
     106
     107        /**
     108         * @return the enumValues
     109         */
     110        public List<String> getEnumValues() {
     111                return enumValues;
     112        }
     113
     114        /**
     115         * @return the procedureSignature
     116         */
     117        public String getProcedureSignature() {
     118                return procedureSignature;
     119        }
     120
     121        /**
     122         * @return the uid
     123         */
     124        public String getUid() {
     125                return uid;
    63126        }
    64127
     
    70133         *             when Param getType is not defined
    71134         */
    72         public Param finish()  {
    73                 assert param != null;
    74                 param.id = id;
    75                 param.name = name;
    76                 param.help = help;
    77                 param.group = group;
    78                 param.flags = flags;
    79                 param.internalId = internalId;
    80                 return param;
     135        public Param finish() {
     136                try {
     137                        return paramType.getConstructor(ParamBuilder.class).newInstance(this);
     138                } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
     139                        throw new FramsticksException().msg("failed to create param").cause(e).arg("name", name);
     140                }
    81141        }
    82142
     
    87147        }
    88148
    89         protected <T extends Param> ParamBuilder internalSetType(Class<? extends Param> type, T param) {
    90                 this.paramType = type;
    91                 this.param = param;
    92                 if (param instanceof PrimitiveParam) {
    93                         primitiveParam = (PrimitiveParam) param;
    94                 }
    95                 typeString = param.getFramsTypeName();
    96                 return this;
    97         }
    98 
    99149        public <T extends Param> ParamBuilder type(Class<T> type) {
    100150                assert type != null;
    101                 try {
    102                         return internalSetType(type, type.newInstance());
    103                 } catch (InstantiationException | IllegalAccessException e) {
    104                         e.printStackTrace();
    105                 }
    106                 return this;
    107         }
    108 
    109         public <T extends Param> ParamBuilder type(T param) {
    110                 return internalSetType(param.getClass(), param);
    111         }
     151                this.paramType = type;
     152                return this;
     153        }
     154
     155        // public <T extends Param> ParamBuilder type(T param) {
     156        //      return internalSetType(param.getClass(), param);
     157        // }
    112158
    113159
     
    120166        }
    121167
     168        /**
     169         * @return the internalId
     170         */
     171        public String getInternalId() {
     172                return internalId;
     173        }
     174
    122175        public ParamBuilder setInternalId(String internalId) {
    123176                this.internalId = internalId;
     
    144197
    145198        protected <T extends Number> void parseMinMaxDefNumber(Class<T> type, String second, String third) {
    146                 if (primitiveParam == null) {
    147                         log.warn("param is not a primitive param");
    148                         return;
    149                 }
    150199                if (second != null) {
    151                         this.primitiveParam.min = Numbers.parse(second, type);
    152                         if (this.primitiveParam.min == null) {
    153                                 log.warn("value of min attribute was invalid");
    154                         }
     200                        min = second;
    155201                }
    156202                if (third != null) {
    157                         this.primitiveParam.max = Numbers.parse(third, type);
    158                         if (this.primitiveParam.max == null) {
    159                                 log.warn("value of min attribute was invalid");
    160                         }
    161                 }
    162         }
     203                        max = third;
     204                }
     205        }
     206
     207        protected List<String> enumValues;
     208
     209        public ParamBuilder enums(List<String> values) {
     210                enumValues = values;
     211                return type(EnumParam.class);
     212        }
     213
     214        protected String procedureSignature;
     215
     216        protected String uid;
    163217
    164218        @ParamAnnotation
    165219        public ParamBuilder type(String type) {
    166                 typeString = type;
     220                // typeString = type;
    167221
    168222                log.trace("parsing type: " + type);
     
    175229                switch (first.charAt(0)) {
    176230                        case 'o': {
    177                                 type(new ObjectParam(second != null ? second : first.substring(1)));
     231                                containedTypeName = second != null ? second : first.substring(1);
     232                                type(ObjectParam.class);
    178233                                break;
    179234                        }
    180235                        case 'p': {
    181                                 ProcedureParam procedureParam = new ProcedureParam();
    182                                 String signature = type.substring(1);
    183                                 try {
    184                                         procedureParam.parseSignature(signature);
    185                                 } catch (Exception e) {
    186                                         log.error("invalid procedure signature '" + signature + "': " + e);
    187                                 }
    188                                 type(procedureParam);
     236                                procedureSignature = type.substring(1);
     237                                type(ProcedureParam.class);
    189238                                break;
    190239                        }
     
    193242                                int tildeIndex = type.indexOf("~");
    194243                                if (tildeIndex != -1) {
    195                                         type(new EnumParam(new ArrayList<String>(Arrays.asList(type.substring(tildeIndex + 1).split("~")))));
     244                                        enums(Arrays.asList(type.substring(tildeIndex + 1).split("~")));
    196245                                } else {
    197246                                        if (first.length() >= 2) {
     
    214263                                                type(BooleanParam.class);
    215264                                        }
    216                                         if (param == null) {
     265                                        if (paramType == null) {
    217266                                                type(DecimalParam.class);
    218267                                        }
    219268                                }
    220                                 if (this.param instanceof DecimalParam) {
     269                                if (DecimalParam.class.isAssignableFrom(this.paramType)) {
    221270                                        parseMinMaxDefNumber(Integer.class, second, third);
    222271                                }
     
    243292                        }
    244293                        case 'l': {
    245                                 type(third != null ? new UniqueListParam(second, third) : new ArrayListParam(second));
     294                                containedTypeName = second;
     295                                if (third != null) {
     296                                        type(UniqueListParam.class);
     297                                        uid = third;
     298                                } else {
     299                                        type(ArrayListParam.class);
     300                                }
    246301                                break;
    247302                        }
     
    294349        @ParamAnnotation
    295350        public String getType() {
    296                 return typeString;
     351                return "?";
    297352        }
    298353
     
    304359        }
    305360
    306         public <T> ParamBuilder min(T min) {
    307                 if (primitiveParam != null) {
    308                         this.primitiveParam.min = min;
    309                 }
    310                 return this;
    311         }
    312 
    313         public <T> ParamBuilder max(T max) {
    314                 if (primitiveParam != null) {
    315                         this.primitiveParam.max = max;
    316                 }
    317                 return this;
    318         }
    319 
    320         public <T> ParamBuilder def(T def) {
    321                 if (def != null && primitiveParam != null) {
    322                         this.primitiveParam.def = def;
    323                 }
    324                 return this;
    325         }
    326 
    327         public Class<?> getStorageType() {
    328                 assert param != null;
    329                 return param.getStorageType();
    330         }
     361        public ParamBuilder min(Object min) {
     362                this.min = min;
     363                return this;
     364        }
     365
     366        public ParamBuilder max(Object max) {
     367                this.max = max;
     368                return this;
     369        }
     370
     371        public ParamBuilder def(Object def) {
     372                this.def = def;
     373                return this;
     374        }
     375
     376        // public Class<?> getStorageType() {
     377        //      assert param != null;
     378        //      return param.getStorageType();
     379        // }
    331380
    332381        public Param build(String line) throws Exception {
     
    386435        }
    387436
    388 
     437        public ParamBuilder defaultDef(Object def) {
     438                if (this.def == null) {
     439                        return def(def);
     440                }
     441                return this;
     442        }
    389443
    390444}
Note: See TracChangeset for help on using the changeset viewer.