Ignore:
Timestamp:
06/22/13 21:51:33 (11 years ago)
Author:
psniegowski
Message:

HIGHLIGHTS:

  • simplification of entities management model
  • cleanup around params (improve hierarchy)
  • migrate from JUnit to TestNG
  • introduce FEST to automatically test GUI
  • improve slider control
  • loosen synchronization between gui tree and backend representation
  • and many other bug fixes

NOTICE:

  • a great many of lines is changed only because of substituting spaces with tabs

CHANGELOG (oldest changes at the bottom):

Some cleaning after fix found.

Fix bug with tree.

More changes with TreeNodes?.

Finally fix issue with tree.

Improve gui tree management.

Decouple update of values from fetch request in gui.

Minor changes.

Minor changes.

Minor change.

Change Path construction wording.

More fixes to SliderControl?.

Fix SliderControl?.

Fix SliderControl?.

Minor improvement.

Several changes.

Make NumberParam? a generic class.

Add robot to the gui test.

Setup common testing logging configuration.

Remove Parameters class.

Remove entityOwner from Parameters.

Move name out from Parameters class.

Move configuration to after the construction.

Simplify observers and endpoints.

Remove superfluous configureEntity overrides.

Add dependency on fest-swing-testng.

Use FEST for final print test.

Use FEST for more concise and readable assertions.

Divide test of F0Parser into multiple methods.

Migrate to TestNG

Minor change.

Change convention from LOGGER to log.

Fix reporting of errors during controls filling.

Bound maximal height of SliderControl?.

Minor improvements.

Improve tooltips for controls.

Also use Delimeted in more places.

Move static control utilities to Gui.

Rename package gui.components to controls.

Some cleaning in controls.

Improve Param classes placing.

Move ValueParam?, PrimitiveParam? and CompositeParam? one package up.

Improve ParamBuilder?.

Move getDef to ValueParam? and PrimitiveParam?.

Move getMax and getDef to ValueParam?.

Move getMin to ValueParam?.

Upgrade to laters apache commons versions.

Use filterInstanceof extensively.

Add instanceof filters.

Make ValueParam? in many places of Param.

Place assertions about ValueParam?.

Add ValueParam?

Rename ValueParam? to PrimitiveParam?

Minor changes.

Several improvements to params types.

Add NumberParam?.

Add TextControl? component.

Add .swp files to .gitignore

Greatly improved slider component.

Some improvements.

Make Param.reassign return also a state.

Add IterableIterator?.

Several changes.

  • Move util classes to better packages.
  • Remove warnings from eclim.

Several improvements.

Fix bug with BooleanParam?.

Some experiments with visualization.

Another fix to panel management.

Improve panel management.

Some refactorization around panels.

Add root class for panel.

Location:
java/main
Files:
5 added
2 deleted
31 edited

Legend:

Unmodified
Added
Removed
  • java/main

    • Property svn:ignore set to
      target
  • java/main/src/main/java/com/framsticks/params/AccessInterface.java

    r78 r84  
    22
    33import java.util.Collection;
    4 import java.util.List;
    54
    65
     
    98 * framsticks. In descriptions of methods names "property" and "parameter" are
    109 * synonyms.
    11  * 
     10 *
    1211 * @author Jarek Szymczak <name.surname@gmail.com> (please replace name and
    1312 *         surname with my personal data)
     
    2019
    2120        String getId();
    22 
    23 
    24     /*
    25         String getName();
    26 
    27     * This method provides more verbose (but not strict) names of classes.
    28      *
    29      * e.g. for a list it will return l Type, and if name of FramsClass missing,
    30      * it's id will be used
    31      *
    32     String getNiceName();
    33     */
    3421
    3522        Param getGroupMember(int gi, int n);
     
    4431        <T> T get(String id, Class<T> type);
    4532
    46         <T> T get(Param param, Class<T> type);
     33        <T> T get(ValueParam param, Class<T> type);
    4734
    4835        <T> int set(int i, T value);
     
    5037        <T> int set(String id, T value);
    5138
    52         <T> int set(Param param, T value);
     39        <T> int set(ValueParam param, T value);
    5340
    5441        void setDefault(boolean numericOnly);
     
    7158
    7259        /**
    73          * Return as a single line String.
    74          *
    75          * @param omitDefaultValues
    76          *            the omit default values
    77          * @return the string
    78          */
    79         String save2(boolean omitDefaultValues);
    80 
    81         /**
    8260         * Removes all the properties values.
    8361         */
    8462        void clearValues();
    8563
    86         void select(Object object);
     64        AccessInterface select(Object object);
    8765
    8866        Object getSelected();
     
    9068        AccessInterface cloneAccess();
    9169
    92     Object createAccessee();
     70        Object createAccessee();
    9371
    94     Collection<Param> getParams();
     72        Collection<Param> getParams();
    9573
    96     FramsClass getFramsClass();
     74        FramsClass getFramsClass();
    9775
    9876
  • java/main/src/main/java/com/framsticks/params/ArrayListAccess.java

    r77 r84  
    11package com.framsticks.params;
    22
    3 import com.framsticks.util.Numbers;
     3import com.framsticks.util.lang.Numbers;
    44
    55import java.util.ArrayList;
     
    1313public class ArrayListAccess extends ListAccess {
    1414
    15     List list;
     15        List<Object> list;
    1616
     17        public ArrayListAccess(AccessInterface elementAccess) {
     18                super(elementAccess);
     19        }
    1720
    18     public ArrayListAccess(AccessInterface elementAccess) {
    19         super(elementAccess);
    20     }
     21        @Override
     22        public ArrayListAccess cloneAccess() {
     23                return new ArrayListAccess(elementAccess.cloneAccess());
     24        }
    2125
    22     @Override
    23     public ArrayListAccess cloneAccess() {
    24         return new ArrayListAccess(elementAccess.cloneAccess());
    25     }
     26        @Override
     27        public List<?> createAccessee() {
     28                return new ArrayList<Object>();
     29        }
    2630
    27     @Override
    28     public List createAccessee() {
    29         return new ArrayList();
    30     }
     31        @Override
     32        public Param getParam(int i) {
     33                return new ParamBuilder().setId(Integer.toString(i)).setName(elementAccess.getId()).setType("o " + elementAccess.getId()).build();
     34        }
    3135
    32     @Override
    33     public Param getParam(int i) {
    34         return new ParamBuilder().setId(Integer.toString(i)).setName(elementAccess.getId()).setType("o " + elementAccess.getId()).build();
    35     }
     36        @Override
     37        public Param getParam(String id) {
     38                Integer i = Numbers.parse(id, Integer.class);
     39                return (i == null ? null : getParam(i));
     40        }
    3641
    37     @Override
    38     public Param getParam(String id) {
    39         Integer i = Numbers.parse(id, Integer.class);
    40         return (i == null ? null : getParam(i));
    41     }
     42        @Override
     43        public String getId() {
     44                return "l " + elementAccess.getId();
     45        }
    4246
    43     @Override
    44     public String getId() {
    45         return "l " + elementAccess.getId();
    46     }
     47        @Override
     48        public int getParamCount() {
     49                return list.size();
     50        }
    4751
    48     @Override
    49     public int getParamCount() {
    50         return list.size();
    51     }
     52        @Override
     53        public <T> T get(int i, Class<T> type) {
     54                if (i < list.size()) {
     55                        return type.cast(list.get(i));
     56                }
     57                return null;
     58        }
    5259
    53     @Override
    54     public <T> T get(int i, Class<T> type) {
    55         if (i < list.size()) {
    56             return type.cast(list.get(i));
    57         }
    58         return null;
    59     }
     60        @Override
     61        public <T> T get(String id, Class<T> type) {
     62                return get(Integer.parseInt(id), type);
     63        }
    6064
    61     @Override
    62     public <T> T get(String id, Class<T> type) {
    63         return get(Integer.parseInt(id), type);
    64     }
     65        @Override
     66        public <T> T get(ValueParam param, Class<T> type) {
     67                return get(param.getId(), type);
     68        }
    6569
    66     @Override
    67     public <T> T get(Param param, Class<T> type) {
    68         return get(param.getId(), type);
    69     }
     70        @Override
     71        public <T> int set(int i, T value) {
     72                while (i >= list.size()) {
     73                        list.add(null);
     74                }
     75                list.set(i, value);
     76                return 0;
     77        }
    7078
    71     @Override
    72     public <T> int set(int i, T value) {
    73         while (i >= list.size()) {
    74             list.add(null);
    75         }
    76         list.set(i, value);
    77         return 0;
    78     }
     79        @Override
     80        public <T> int set(String id, T value) {
     81                return set(Integer.parseInt(id), value);
     82        }
    7983
    80     @Override
    81     public <T> int set(String id, T value) {
    82         return set(Integer.parseInt(id), value);
    83     }
     84        @Override
     85        public <T> int set(ValueParam param, T value) {
     86                return set(param.getId(), value);
     87        }
    8488
    85     @Override
    86     public <T> int set(Param param, T value) {
    87         return set(param.getId(), value);
    88     }
     89        @Override
     90        public void clearValues() {
     91                list.clear();
     92        }
    8993
    90     @Override
    91     public void clearValues() {
    92         list.clear();
    93     }
     94        /** covariant */
     95        @Override
     96        public List<Object> getSelected() {
     97                return list;
     98        }
    9499
    95     /** covariant */
    96     @Override
    97     public List getSelected() {
    98         return list;
    99     }
     100        @SuppressWarnings("unchecked")
     101        @Override
     102        public ArrayListAccess select(Object object) {
     103                list = (List<Object>) object;
     104                return this;
     105        }
    100106
    101     @Override
    102     public void select(Object object) {
    103         list = (List)object;
    104     }
     107        @Override
     108        public String computeIdentifierFor(Object selected) {
     109                return Integer.toString(list.size());
     110        }
    105111
    106     @Override
    107     public String computeIdentifierFor(Object selected) {
    108         return Integer.toString(list.size());
    109     }
    110 
    111     @Override
    112     public Collection<Param> getParams() {
    113         List<Param> result = new LinkedList<Param>();
    114         if (list == null) {
    115             return result;
    116         }
    117         for (int i = 0; i < list.size(); ++i) {
    118             result.add(getParam(i));
    119         }
    120         return result;
    121     }
     112        @Override
     113        public Collection<Param> getParams() {
     114                List<Param> result = new LinkedList<Param>();
     115                if (list == null) {
     116                        return result;
     117                }
     118                for (int i = 0; i < list.size(); ++i) {
     119                        result.add(getParam(i));
     120                }
     121                return result;
     122        }
    122123
    123124
  • java/main/src/main/java/com/framsticks/params/CastFailure.java

    r77 r84  
    55 */
    66public class CastFailure extends Exception {
     7
     8        /**
     9         *
     10         */
     11        private static final long serialVersionUID = 2117813329617553801L;
     12
    713}
  • java/main/src/main/java/com/framsticks/params/Flags.java

    r77 r84  
    33import java.lang.reflect.Field;
    44import org.apache.log4j.Logger;
     5
     6import com.framsticks.util.lang.Delimeted;
    57
    68/**
     
    911 *
    1012 * Based on c++ defines located in: cpp/gdk/param.h
    11  * 
     13 *
    1214 * @author Jarek Szymczak <name.surname@gmail.com>
    1315 * (please replace name and surname with my personal data)
     
    1719public final class Flags {
    1820
    19         private final static Logger logger = Logger.getLogger(Flags.class.getName());
    20 
     21        private final static Logger log = Logger.getLogger(Flags.class.getName());
    2122
    2223        public static final int READONLY = 1;
     
    4849        public static final int PSET_HITMAX = 8;
    4950
     51        public static String write(int flags, String empty) {
     52                Delimeted d = new Delimeted("+", empty);
     53                try {
     54                        for (Field f : Flags.class.getDeclaredFields()) {
     55                                if (f.getType() != int.class) {
     56                                        continue;
     57                                }
     58                                if ((flags & f.getInt(null)) != 0) {
     59                                        d.append(f.getName());
     60                                }
     61                        }
     62                } catch (IllegalArgumentException e) {
     63                        e.printStackTrace();
     64                } catch (IllegalAccessException e) {
     65                        e.printStackTrace();
     66                }
     67                return d.build();
     68        }
     69
    5070        public static Integer read(String flags) {
    5171                Integer allFlags = 0;
     
    5979                                        allFlags |= Integer.parseInt(field.get(null).toString());
    6080                                } catch (SecurityException e) {
    61                                         logger.warn("security exception was thrown while trying to read flag ("
    62                                                         + flag + ")");
     81                                        log.warn("security exception was thrown while trying to read flag (" + flag + ")");
    6382                                } catch (NoSuchFieldException e) {
    64                                         logger.warn("selected flag is not known (" + flag + ")");
     83                                        log.warn("selected flag is not known (" + flag + ")");
    6584                                } catch (IllegalArgumentException e) {
    66                                         logger.warn("selected flag is not known (" + flag + ")");
     85                                        log.warn("selected flag is not known (" + flag + ")");
    6786                                } catch (IllegalAccessException e) {
    68                                         logger.warn("selected flag is not known (" + flag + ")");
     87                                        log.warn("selected flag is not known (" + flag + ")");
    6988                                }
    7089                        }
  • java/main/src/main/java/com/framsticks/params/FramsClass.java

    r78 r84  
    11package com.framsticks.params;
    22
    3 import com.framsticks.params.types.CompositeParam;
    4 import com.framsticks.params.types.DecimalParam;
    5 import com.framsticks.params.types.FloatParam;
    63import com.framsticks.params.types.StringParam;
    74import com.framsticks.parsers.FileSource;
    85import com.framsticks.parsers.Loaders;
    9 import com.framsticks.util.Casting;
     6import com.framsticks.util.lang.Casting;
     7
    108import org.apache.log4j.Logger;
    119
    12 import javax.lang.model.element.TypeElement;
    1310import java.io.InputStream;
    1411import java.lang.reflect.*;
    1512import java.util.*;
    16 import java.util.List;
    1713
    1814/**
     
    3026public final class FramsClass {
    3127
    32     private final static Logger LOGGER = Logger.getLogger(FramsClass.class.getName());
    33 
    34     /**
    35       * The Class which represents group.
    36       */
     28        private final static Logger log = Logger.getLogger(FramsClass.class.getName());
     29
     30        /**
     31          * The Class which represents group.
     32          */
    3733
    3834        /** The offset of the parameter (applied for newly added parameter). */
     
    7571        /**
    7672         * Adds new param entry.
    77          * 
     73         *
    7874         * @param param
    7975         *            the new param entry
     
    113109        /**
    114110         * Gets the group member.
    115          * 
     111         *
    116112         * @param gi
    117113         *            the offset of group
     
    123119                if (gi < 0 || pi < 0 || gi >= groups.size()) {
    124120                        return null;
    125         }
     121                }
    126122                Group group = groups.get(gi);
    127123                return (group != null ? group.getProperty(pi) : null);
     
    130126        /**
    131127         * Gets the group getName.
    132          * 
     128         *
    133129         * @param gi
    134130         *            the offset of group
     
    149145        }
    150146
    151     public String getNiceName() {
    152         return name != null ? name : id;
    153     }
     147        public String getNiceName() {
     148                return name != null ? name : id;
     149        }
    154150
    155151        /**
    156152         * Gets the param entry.
    157          * 
     153         *
    158154         * @param i
    159155         *            the offset of parameter
    160156         * @return the param entry
    161157         */
    162         public Param getParamEntry(int i) {
     158        public <T extends Param> T getParamEntry(int i, Class<T> type) {
    163159                if (i < 0 || i >= paramList.size()) {
    164160                        return null;
    165161                }
    166                 return paramList.get(i);
     162                return Casting.tryCast(type, paramList.get(i));
    167163        }
    168164
    169165        /**
    170166         * Gets the param entry.
    171          * 
     167         *
    172168         * @param id
    173169         *            the getId of parameter
    174170         * @return the param entry
    175171         */
    176         public Param getParamEntry(String id) {
    177                 return paramEntryMap.get(id);
     172        public <T extends Param> T getParamEntry(String id, Class<T> type) {
     173                return Casting.tryCast(type, paramEntryMap.get(id));
    178174        }
    179175
     
    206202        }
    207203
    208     public static String getParamTypeForNativeType(Type type) {
    209         if (type instanceof ParameterizedType) {
    210             ParameterizedType p = (ParameterizedType) type;
    211             Type rawType = p.getRawType();
    212             if (rawType.equals(Map.class)) {
    213                 Type containedType = p.getActualTypeArguments()[1];
    214                 //TODO uid should be passed along during construction
    215                 if (containedType instanceof Class) {
    216                     return "l " + ((Class) containedType).getCanonicalName() + " name";
    217                 }
    218             }
     204        public static String getParamTypeForNativeType(Type type) {
     205                if (type instanceof ParameterizedType) {
     206                        ParameterizedType p = (ParameterizedType) type;
     207                        Type rawType = p.getRawType();
     208                        if (rawType.equals(Map.class)) {
     209                                Type containedType = p.getActualTypeArguments()[1];
     210                                //TODO uid should be passed along during construction
     211                                if (containedType instanceof Class) {
     212                                        return "l " + ((Class<?>) containedType).getCanonicalName() + " name";
     213                                }
     214                        }
    219215                        if (rawType.equals(List.class)) {
    220216                                Type containedType = p.getActualTypeArguments()[0];
    221217                                if (containedType instanceof Class) {
    222                                         return "l " + ((Class) containedType).getCanonicalName();
    223                                 }
    224                         }
    225             return null;
    226         }
    227 
    228         if (type.equals(Integer.class)) {
    229             return "d";
    230         }
    231         if (type.equals(String.class)) {
    232             return "s";
    233         }
    234         if (type.equals(Double.class)) {
    235             return "f";
    236         }
    237         if (type instanceof Class) {
    238             return "o " + ((Class) type).getCanonicalName();
    239         }
    240         return null;
    241     }
    242 
    243     public static final String GENERATE_HELP_PREFIX = "automatically generated from: ";
     218                                        return "l " + ((Class<?>) containedType).getCanonicalName();
     219                                }
     220                        }
     221                        return null;
     222                }
     223
     224                if (type.equals(Integer.class)) {
     225                        return "d";
     226                }
     227                if (type.equals(String.class)) {
     228                        return "s";
     229                }
     230                if (type.equals(Double.class)) {
     231                        return "f";
     232                }
     233                if (type instanceof Class) {
     234                        return "o " + ((Class<?>) type).getCanonicalName();
     235                }
     236                return null;
     237        }
     238
     239        public static final String GENERATE_HELP_PREFIX = "automatically generated from: ";
    244240
    245241        public static FramsClass readFromStream(InputStream stream) {
     
    248244
    249245        public static class Constructor {
    250         protected final FramsClass result;
    251         protected Class currentClass;
    252         public Constructor(Class src, String name) {
    253             result = new FramsClass(name, name, GENERATE_HELP_PREFIX + src.toString());
    254             currentClass = src;
    255             while (currentClass != null) {
    256                 try {
    257                     currentClass.getMethod("constructFramsClass", Constructor.class).invoke(null, this);
    258                 } catch (Exception ignored) {
    259                 }
    260                 currentClass = currentClass.getSuperclass();
    261             }
     246                protected final FramsClass result;
     247                protected Class<?> currentClass;
     248
     249                public Constructor(Class<?> src, String name) {
     250                        result = new FramsClass(name, name, GENERATE_HELP_PREFIX + src.toString());
    262251                        currentClass = src;
    263         }
    264 
    265         public final FramsClass getResult() {
    266             return result;
    267         }
     252                        while (currentClass != null) {
     253                                try {
     254                                        currentClass.getMethod("constructFramsClass", Constructor.class).invoke(null, this);
     255                                } catch (Exception ignored) {
     256                                }
     257                                currentClass = currentClass.getSuperclass();
     258                        }
     259                        currentClass = src;
     260                }
     261
     262                public final FramsClass getResult() {
     263                        return result;
     264                }
    268265
    269266                public Constructor allFields() {
     
    274271                }
    275272
    276         public Constructor method(String name, Class<?> ... arguments) {
    277             try {
    278                 Method method = currentClass.getMethod(name, arguments);
    279                 if (!Modifier.isPublic(method.getModifiers())) {
    280                     return this;
    281                 }
    282                 String returnParamClass = getParamTypeForNativeType(method.getGenericReturnType());
    283                 if (returnParamClass == null) {
    284                     return this;
    285                 }
    286                 Class[] args = method.getParameterTypes();
    287                 if (args.length == 0) {
    288                     if (method.getName().startsWith("get")) {
    289                         String fieldName = method.getName().substring(3, 4).toLowerCase() + method.getName().substring(4);
    290                         Param param = new ParamBuilder().setType(returnParamClass).setName(fieldName).setId(fieldName).setHelp(GENERATE_HELP_PREFIX + method.toString()).build();
    291                         assert param != null;
    292                         result.append(param);
    293                         return this;
    294                     }
    295                     return this;
    296                 }
    297                 return this;
    298             } catch (NoSuchMethodException e) {
    299                 LOGGER.fatal("method " + name + " was not found in " + currentClass.toString());
    300             }
    301             return this;
    302         }
    303         public Constructor field(String name) {
    304             try {
    305                 Field field = currentClass.getField(name);
    306                 if (!Modifier.isPublic(field.getModifiers())) {
    307                     return this;
    308                 }
    309                 String paramClass = getParamTypeForNativeType(field.getGenericType());
    310                 if (paramClass == null) {
    311                     return this;
    312                 }
    313                 Param param = new ParamBuilder().setType(paramClass).setName(field.getName()).setId(field.getName()).setHelp(GENERATE_HELP_PREFIX + field.toString()).build();
    314                 assert param != null;
    315                 result.append(param);
    316                 return this;
    317             } catch (NoSuchFieldException e) {
    318                 LOGGER.fatal("field " + name + " was not found in " + currentClass.toString());
    319             }
    320             return this;
    321         }
    322     }
     273                public Constructor method(String name, Class<?> ... arguments) {
     274                        try {
     275                                Method method = currentClass.getMethod(name, arguments);
     276                                if (!Modifier.isPublic(method.getModifiers())) {
     277                                        return this;
     278                                }
     279                                String returnParamClass = getParamTypeForNativeType(method.getGenericReturnType());
     280                                if (returnParamClass == null) {
     281                                        return this;
     282                                }
     283                                Class<?>[] args = method.getParameterTypes();
     284                                if (args.length == 0) {
     285                                        if (method.getName().startsWith("get")) {
     286                                                String fieldName = method.getName().substring(3, 4).toLowerCase() + method.getName().substring(4);
     287                                                Param param = new ParamBuilder().setType(returnParamClass).setName(fieldName).setId(fieldName).setHelp(GENERATE_HELP_PREFIX + method.toString()).build();
     288                                                assert param != null;
     289                                                result.append(param);
     290                                                return this;
     291                                        }
     292                                        return this;
     293                                }
     294                                return this;
     295                        } catch (NoSuchMethodException e) {
     296                                log.fatal("method " + name + " was not found in " + currentClass.toString());
     297                        }
     298                        return this;
     299                }
     300                public Constructor field(String name) {
     301                        try {
     302                                Field field = currentClass.getField(name);
     303                                if (!Modifier.isPublic(field.getModifiers())) {
     304                                        return this;
     305                                }
     306                                String paramClass = getParamTypeForNativeType(field.getGenericType());
     307                                if (paramClass == null) {
     308                                        return this;
     309                                }
     310                                Param param = new ParamBuilder().setType(paramClass).setName(field.getName()).setId(field.getName()).setHelp(GENERATE_HELP_PREFIX + field.toString()).build();
     311                                assert param != null;
     312                                result.append(param);
     313                                return this;
     314                        } catch (NoSuchFieldException e) {
     315                                log.fatal("field " + name + " was not found in " + currentClass.toString());
     316                        }
     317                        return this;
     318                }
     319        }
    323320}
  • java/main/src/main/java/com/framsticks/params/ListAccess.java

    r78 r84  
    11package com.framsticks.params;
    22
    3 import com.framsticks.core.Node;
    4 import com.framsticks.core.Path;
    5 import com.framsticks.params.types.CompositeParam;
    63
    7 import java.util.List;
     4
     5import static com.framsticks.util.lang.Containers.filterInstanceof;
    86
    97/**
     
    1816        }
    1917
    20     @Override
     18        @Override
    2119        public Param getGroupMember(int gi, int n) {
    2220                return null;
     
    5250
    5351        @Override
    54     public void save(SinkInterface sink) {
    55         for (Param p : getParams()) {
    56             assert p instanceof CompositeParam;
    57             CompositeParam childParam = (CompositeParam)p;
    58             Object child = get(childParam, Object.class);
    59             //this is probably an assertion
    60             assert child != null;
    61             elementAccess.select(child);
    62             elementAccess.save(sink);
    63         }
    64     }
     52        public void save(SinkInterface sink) {
     53                for (CompositeParam p : filterInstanceof(getParams(), CompositeParam.class)) {
     54                        Object child = get(p, Object.class);
     55                        //this is probably an assertion
     56                        assert child != null;
     57                        elementAccess.select(child);
     58                        elementAccess.save(sink);
     59                }
     60        }
    6561
    6662        @Override
    6763        public void load(SourceInterface stream) throws Exception {
    68         }
    69 
    70         @Override
    71         public String save2(boolean omitDefaultValues) {
    72                 return null;
    7364        }
    7465
     
    7768        }
    7869
    79     public abstract String computeIdentifierFor(Object selected);
     70        public abstract String computeIdentifierFor(Object selected);
    8071
    81     @Override
    82     public final FramsClass getFramsClass() {
    83         return elementAccess.getFramsClass();
    84     }
     72        @Override
     73        public final FramsClass getFramsClass() {
     74                return elementAccess.getFramsClass();
     75        }
    8576
    8677}
  • java/main/src/main/java/com/framsticks/params/ListSink.java

    r77 r84  
    88 */
    99public class ListSink implements SinkInterface {
    10     protected final List<String> out;
    11     protected StringBuilder lineBuilder = new StringBuilder();
     10        protected final List<String> out;
     11        protected StringBuilder lineBuilder = new StringBuilder();
    1212
    13     public ListSink(List<String> out) {
    14         this.out = out;
    15     }
     13        public ListSink(List<String> out) {
     14                this.out = out;
     15        }
    1616
    17     public ListSink() {
    18         this.out = new LinkedList<String>();
    19     }
     17        public ListSink() {
     18                this.out = new LinkedList<String>();
     19        }
    2020
    21     public final List<String> getOut() {
    22         return out;
    23     }
     21        public final List<String> getOut() {
     22                return out;
     23        }
    2424
    2525
    26     @Override
    27     public SinkInterface print(String str) {
    28         lineBuilder.append(str);
    29         return this;
    30     }
     26        @Override
     27        public SinkInterface print(String str) {
     28                lineBuilder.append(str);
     29                return this;
     30        }
    3131
    32     @Override
    33     public SinkInterface print(Object obj) {
    34         lineBuilder.append(obj);
    35         return this;
    36     }
     32        @Override
     33        public SinkInterface print(Object obj) {
     34                lineBuilder.append(obj);
     35                return this;
     36        }
    3737
    38     @Override
    39     public void breakLine() {
    40         out.add(lineBuilder.toString());
    41         lineBuilder = new StringBuilder();
    42     }
     38        @Override
     39        public void breakLine() {
     40                out.add(lineBuilder.toString());
     41                lineBuilder = new StringBuilder();
     42        }
    4343
    44     @Override
    45     public void close() {
    46     }
     44        @Override
     45        public void close() {
     46        }
    4747}
  • java/main/src/main/java/com/framsticks/params/ListSource.java

    r77 r84  
    77
    88        private Iterator<String> iterator = null;
    9     private final List<String> source;
     9    //private final List<String> source;
    1010
    1111        public ListSource(List<String> source) {
    12         this.source = source;
     12        //this.source = source;
    1313        iterator = source.iterator();
    1414        }
  • java/main/src/main/java/com/framsticks/params/Param.java

    r78 r84  
    33import com.framsticks.params.types.DecimalParam;
    44import com.framsticks.params.types.StringParam;
    5 
    6 import java.util.HashMap;
    7 import java.util.Map;
    85
    96/**
     
    4239        protected Integer extra;
    4340
    44         /** The minimum allowed value of parameter. */
    45         protected Object min;
    46 
    47         /** The maximum allowed value of parameter. */
    48         protected Object max;
    49 
    50         /** The default value of parameter. */
    51         protected Object def;
    52 
    53 
    54 
    55         /**
    56          * The defaults used for Integer, Double and String classes, if the default
    57          * value of parameter is not explicitly given.
    58          */
    59         @SuppressWarnings("rawtypes")
    60         private Map<Class, Object> defaults = new HashMap<Class, Object>();
    61         // static initialization of defaults
    62         {
    63                 defaults.put(String.class, "");
    64                 defaults.put(Integer.class, 0);
    65                 defaults.put(Double.class, 0.0);
    66         }
    6741
    6842        public Param() {
     
    9468        }
    9569
    96     public abstract String getType();
     70        public abstract String getFramsTypeName();
    9771
    9872
     
    10175        }
    10276
    103 
    104     private <T> T tryCastAndReturn(Object value, Class<T> type) {
    105         if (value == null)
    106             return null;
    107         try {
    108             return type.cast(value);
    109         } catch (ClassCastException e) {
    110             throw new ClassCastException("property \"" + name
    111                     + "\" type is \"" + value.getClass().getName()
    112                     + "\", not \"" + type.getName() + "\"");
    113         }
    114     }
    115         /**
    116          * Gets the minimum value of parameter.
    117          *
    118          * @param <T> the generic getType which must be correctly specified by user
    119          * @param type the getType of variable, can be checked by
    120          * @return the minimum allowed value of parameter
    121          * @throws ClassCastException the class cast exception, thrown when given getType is incorrect
    122          */
    123         public <T> T getMin(Class<T> type) {
    124                 return tryCastAndReturn(min, type);
    125         }
    126 
    127         /**
    128          * Gets the maximum value of parameter.
    129          *
    130          * @param <T> the generic getType which must be correctly specified by user
    131          * @param type the getType of variable, can be checked by {@link #getStorageType()}
    132          * @return the maximum allowed value of parameter
    133          * @throws ClassCastException the class cast exception, thrown when given getType is incorrect
    134          */
    135         public <T> T getMax(Class<T> type) {
    136                 return tryCastAndReturn(max, type);
    137         }
    138 
    139         /**
    140          * Gets the default value of parameter.
    141          *
    142          * @param <T> the generic getType which must be correctly specified by user
    143          * @param type the getType of variable, can be checked by {@link #getStorageType()}
    144          * @return the default value of parameter
    145          * @throws ClassCastException the class cast exception, thrown when given getType is incorrect
    146          */
    147         public <T> T getDef(Class<T> type) throws ClassCastException {
    148                 return tryCastAndReturn(def, type);
    149         }
    15077
    15178        public Integer getExtra() {
     
    16693        }
    16794
    168         public boolean isEmptyAvailable() {
    169                 return false;
    170         }
    171 
    172         public abstract Class getStorageType();
     95        public abstract Class<?> getStorageType();
    17396
    17497        public boolean hasFlag(int flag) {
     
    17699        }
    177100
    178         public Object reassign(Object newValue, Object oldValue) throws CastFailure {
    179         if (newValue == null) {
    180             if (isEmptyAvailable()) {
    181                 return null;
    182             }
    183             throw new CastFailure();
    184         }
    185         try {
    186             return getStorageType().cast(newValue);
    187         } catch (ClassCastException ignored) {
    188             throw new CastFailure();
    189         }
    190     }
    191 
    192101        public boolean isUserHidden() {
    193102                return (flags & Flags.USERHIDDEN) != 0;
    194103        }
    195104
    196     public static FramsClass getFramsClass() {
    197         return new FramsClass("prop", "prop", null)
    198                 .append(new ParamBuilder().setId("name").setName("Name").setType(StringParam.class).build())
    199                 .append(new ParamBuilder().setId("id").setName("Id").setType(StringParam.class).build())
    200                 .append(new ParamBuilder().setId("type").setName("Type").setType(StringParam.class).build())
    201                 .append(new ParamBuilder().setId("help").setName("Help").setType(StringParam.class).build())
    202                 .append(new ParamBuilder().setId("flags").setName("Flags").setType(DecimalParam.class).build());
    203     }
     105        public static FramsClass getFramsClass() {
     106                return new FramsClass("prop", "prop", null)
     107                                .append(new ParamBuilder().setId("name").setName("Name").setType(StringParam.class).build())
     108                                .append(new ParamBuilder().setId("id").setName("Id").setType(StringParam.class).build())
     109                                .append(new ParamBuilder().setId("type").setName("Type").setType(StringParam.class).build())
     110                                .append(new ParamBuilder().setId("help").setName("Help").setType(StringParam.class).build())
     111                                .append(new ParamBuilder().setId("flags").setName("Flags").setType(DecimalParam.class).build());
     112        }
    204113
    205114}
  • java/main/src/main/java/com/framsticks/params/ParamBuilder.java

    r77 r84  
    22
    33import com.framsticks.params.types.*;
     4import com.framsticks.util.lang.Numbers;
     5
    46import org.apache.log4j.Logger;
    57
     
    911/**
    1012 * The class ParamBuilder helps building Param objects.
    11  * 
     13 *
    1214 * @author Mateusz Jarus <name.surname@gmail.com> (please replace name and
    1315 *         surname with my personal data)
     
    1719
    1820public class ParamBuilder {
    19         private final static Logger LOGGER = Logger.getLogger(ParamBuilder.class.getName());
     21        private final static Logger log = Logger.getLogger(ParamBuilder.class.getName());
    2022
    2123        private static final String ID_FIELD = "id";
     
    4850
    4951        /** The getType of parameter. */
    50         @SuppressWarnings("rawtypes")
    51         private Class paramType;
     52        private Class<? extends Param> paramType;
    5253
    5354        private Param param;
     55        private PrimitiveParam primitiveParam;
    5456
    5557        /**
    5658         * Build Param based on provided data.
    57          * 
     59         *
    5860         * @return Param object
    5961         * @throws Exception
     
    6163         */
    6264        public Param build()  {
    63         assert param != null;
     65                assert param != null;
    6466                param.id = id;
    6567                param.name = name;
     
    6870                param.flags = flags;
    6971                param.internalId = internalId;
    70 
    7172                return param;
    7273        }
     
    7778        }
    7879
     80        protected <T extends Param> ParamBuilder internalSetType(Class<? extends Param> type, T param) {
     81                this.paramType = type;
     82                this.param = param;
     83                if (param instanceof PrimitiveParam) {
     84                        primitiveParam = (PrimitiveParam) param;
     85                }
     86                return this;
     87        }
     88
    7989        public <T extends Param> ParamBuilder setType(Class<T> type) {
    80                 this.paramType = type;
    8190                try {
    82                         this.param = (Param)paramType.newInstance();
     91                        return internalSetType(type, type.newInstance());
    8392                } catch (InstantiationException e) {
    8493                        e.printStackTrace();
     
    8998        }
    9099
    91         public <T extends Param> ParamBuilder setType(T type) {
    92                 this.paramType = type.getClass();
    93                 this.param = type;
    94                 return this;
     100        public <T extends Param> ParamBuilder setType(T param) {
     101                return internalSetType(param.getClass(), param);
    95102        }
    96103
     
    116123        }
    117124
     125        protected <T extends Number> void parseMinMaxDefNumber(Class<T> type, String second, String third) {
     126                if (primitiveParam == null) {
     127                        log.warn("param is not a primitive param");
     128                        return;
     129                }
     130                if (second != null) {
     131                        this.primitiveParam.min = Numbers.parse(second, type);
     132                        if (this.primitiveParam.min == null) {
     133                                log.warn("value of min attribute was invalid");
     134                        }
     135                }
     136                if (third != null) {
     137                        this.primitiveParam.max = Numbers.parse(third, type);
     138                        if (this.primitiveParam.max == null) {
     139                                log.warn("value of min attribute was invalid");
     140                        }
     141                }
     142        }
     143
    118144        public ParamBuilder setType(String type) {
    119145
    120                 LOGGER.trace("parsing type: " + type);
     146                log.trace("parsing type: " + type);
    121147
    122148                String[] typeSplitted = type.split(" ");
     
    136162                                        procedureParam.parseSignature(signature);
    137163                                } catch (Exception e) {
    138                                         LOGGER.error("invalid procedure signature '" + signature + "': " + e);
     164                                        log.error("invalid procedure signature '" + signature + "': " + e);
    139165                                }
    140166                                setType(procedureParam);
     
    148174                                } else {
    149175                                        if (first.length() >= 2) {
    150                                                 switch (first.charAt(1)) {
     176                                switch (first.charAt(1)) {
    151177                                                        case 'b': {
    152178                                                                setType(BinaryParam.class);
     
    159185                                                }
    160186                                        }
    161                     if ("0".equals(second) && "1".equals(third)) {
    162                         setType(BooleanParam.class);
    163                     }
     187                                        if ("0".equals(second) && "1".equals(third)) {
     188                                                setType(BooleanParam.class);
     189                                        }
    164190                                        if (param == null) {
    165191                                                setType(DecimalParam.class);
     
    167193                                }
    168194                                if (this.param instanceof DecimalParam) {
    169                                         try {
    170                                                 if (second != null) {
    171                                                         this.param.min = Integer.parseInt(second);
    172                                                 }
    173 
    174                                                 if (third != null) {
    175                                                         this.param.max = Integer.parseInt(third);
    176                                                 }
    177                                         } catch (NumberFormatException e) {
    178                                                 LOGGER.warn("value of min or max attribute should be an Integer");
    179                                         }
     195                                        parseMinMaxDefNumber(Integer.class, second, third);
    180196                                }
    181197                                break;
     
    183199                        case 'f': {
    184200                                setType(FloatParam.class);
    185                                 try {
    186                                         if (second != null) {
    187                                                 this.param.min = Double.parseDouble(second);
    188                                         }
    189 
    190                                         if (third != null) {
    191                                                 this.param.max = Double.parseDouble(third);
    192                                         }
    193                                 } catch (NumberFormatException ex) {
    194                                         LOGGER.warn("Value of min or max attribute should be Double");
    195                                 }
     201                                parseMinMaxDefNumber(Double.class, second, third);
    196202                                break;
    197203                        }
     
    202208                        case 's': {
    203209                                setType(StringParam.class);
    204                                 this.param.min = second;
    205                                 this.param.max = third;
     210                                setMin(second);
     211                                setMax(third);
    206212                                break;
    207213                        }
     
    211217                        }
    212218                        case 'l': {
    213 
    214219                                setType(third != null ? new UniqueListParam(second, third) : new ArrayListParam(second));
    215220                                break;
     
    225230        }
    226231
     232        /**
     233         * @return the paramType
     234         */
     235        public Class<? extends Param> getParamType() {
     236                return paramType;
     237        }
     238
    227239        public <T> ParamBuilder setMin(T min) {
    228                 this.param.min = min;
     240                if (primitiveParam != null) {
     241                        this.primitiveParam.min = min;
     242                }
    229243                return this;
    230244        }
    231245
    232246        public <T> ParamBuilder setMax(T max) {
    233                 this.param.max = max;
     247                if (primitiveParam != null) {
     248                        this.primitiveParam.max = max;
     249                }
    234250                return this;
    235251        }
    236252
    237253        public <T> ParamBuilder setDef(T def) {
    238                 this.param.def = def;
    239                 return this;
     254                if (def != null && primitiveParam != null) {
     255                        this.primitiveParam.def = def;
     256                }
     257                return this;
     258        }
     259
     260        public Class<?> getStorageType() {
     261                assert param != null;
     262                return param.getStorageType();
    240263        }
    241264
     
    244267
    245268                if (paramEntryValues.length == 0) {
    246                         LOGGER.warn("field empty or wrong format (" + line
     269                        log.warn("field empty or wrong format (" + line
    247270                                        + ") - omitting");
    248271                        return null;
     
    263286                        /** everything is ok, parameters have just finished*/
    264287                } catch (NumberFormatException ex) {
    265                         LOGGER.warn("wrong format of entry: " + line
     288                        log.warn("wrong format of entry: " + line
    266289                                        + ", omitting");
    267290                        return null;
  • java/main/src/main/java/com/framsticks/params/PropertiesAccess.java

    r78 r84  
    11package com.framsticks.params;
    22
    3 import java.util.ArrayList;
    43import java.util.HashMap;
    5 import java.util.List;
    64import java.util.Map;
     5
    76
    87/**
    98 * The Class PropertiesAccess.
    10  * 
     9 *
    1110 * @author Jarek Szymczak <name.surname@gmail.com> (please replace name and
    1211 *         surname with my personal data)
     
    1817        public Map<String, Object> properties;
    1918
    20     @Override
     19        @Override
    2120        public Map<String, Object> createAccessee() {
    2221                return PropertiesAccess.createPropertiesMap();
     
    3332        @Override
    3433        public void clearValues() {
    35         assert properties != null;
    36         properties.clear();
     34                assert properties != null;
     35                properties.clear();
    3736        }
    3837
    3938        @Override
    40         public <T> T get(Param param, Class<T> type) {
    41         assert properties != null;
    42         assert param != null;
     39        public <T> T get(ValueParam param, Class<T> type) {
     40                assert properties != null;
     41                assert param != null;
    4342                Object object = null;
    4443                try {
     
    5453        }
    5554
    56         protected <T> void internalSet(Param param, T value) {
     55        @Override
     56        protected <T> void internalSet(ValueParam param, T value) {
    5757                properties.put(param.getId(), value);
    5858        }
     
    6363         * correct exceptions might occurred while getting / setting the parameters
    6464         * values
    65          * 
     65         *
    6666         * @param object
    6767         *            the properties with parameters values
    6868         */
     69        @SuppressWarnings("unchecked")
    6970        @Override
    70         public void select(Object object) {
     71        public PropertiesAccess select(Object object) {
    7172                this.properties = (Map<String, Object>)object;
     73                return this;
    7274        }
    7375
     
    8183        public PropertiesAccess cloneAccess() {
    8284                return new PropertiesAccess(framsClass);
    83                 //properties = createProperties();
    84                 //return access;
    8585        }
    8686
  • java/main/src/main/java/com/framsticks/params/ReflectionAccess.java

    r78 r84  
    44import java.lang.reflect.InvocationTargetException;
    55import java.lang.reflect.Modifier;
    6 import java.lang.reflect.Type;
    7 import java.util.List;
    86
    9 import com.framsticks.params.types.ListParam;
    107import org.apache.log4j.Logger;
     8
     9import com.framsticks.util.lang.Containers;
    1110
    1211
    1312/**
    1413 * The Class ReflectionAccess. Stores data in provided object using reflection.
    15  * 
     14 *
    1615 * @author Mateusz Jarus <name.surname@gmail.com> (please replace name and
    1716 *         surname with my personal data)
     
    2019 */
    2120public class ReflectionAccess extends SimpleAbstractAccess {
    22         private final static Logger LOGGER = Logger.getLogger(ReflectionAccess.class
    23                         .getName());
     21        private final static Logger log = Logger.getLogger(ReflectionAccess.class.getName());
    2422
    25 
    26     protected final Class reflectedClass;
     23        protected final Class<?> reflectedClass;
    2724        private Object object;
    2825
    29         public ReflectionAccess(Class reflectedClass, FramsClass framsClass) {
    30         this.reflectedClass = reflectedClass;
     26        public ReflectionAccess(Class<?> reflectedClass, FramsClass framsClass) {
     27                this.reflectedClass = reflectedClass;
    3128                setFramsClass(framsClass);
    3229        }
     
    3734
    3835        @Override
    39         public <T> T get(Param param, Class<T> type) {
     36        public <T> T get(ValueParam param, Class<T> type) {
    4037                if (object == null) {
    4138                        return null;
     
    5451                        } catch (InvocationTargetException e) {
    5552                                //e.printStackTrace();
    56             }
     53                        }
    5754
    5855                } catch (IllegalAccessException ex) {
    59                         LOGGER.warn("illegal access error occurred while trying to access returnedObject");
     56                        log.warn("illegal access error occurred while trying to access returnedObject");
    6057                        ex.printStackTrace();
    61         } catch (ClassCastException ignored) {
     58                } catch (ClassCastException ignored) {
    6259
    63         }
     60                }
    6461                return null;
    6562        }
    6663
    6764        @Override
    68         protected <T> void internalSet(Param param, T value) {
     65        protected <T> void internalSet(ValueParam param, T value) {
    6966                setValue(param, value);
    7067        }
    7168
    72         private <T> void setValue(Param param, T value) {
     69        private <T> void setValue(ValueParam param, T value) {
    7370                if (object == null) {
    7471                        return;
    75         }
     72                }
    7673                try {
    7774                        String id = param.getId();
    7875                        try {
    7976                                Field f = reflectedClass.getField(id);
    80                                 Class t = f.getType();
     77                                Class<?> t = f.getType();
    8178                                if (Modifier.isFinal(f.getModifiers())) {
    8279                                        return;
     
    111108
    112109                try {
    113                         for (Param p : framsClass.getParamEntries()) {
     110                        for (ValueParam p : Containers.filterInstanceof(framsClass.getParamEntries(), ValueParam.class)) {
    114111                                setValue(p, p.getDef(Object.class));
    115112                        }
     
    121118        /**
    122119         * Sets the new object to operate on.
    123          * 
     120         *
    124121         * @param object
    125122         *            new object to operate on
    126123         */
    127124        @Override
    128         public void select(Object object) {
     125        public ReflectionAccess select(Object object) {
    129126                assert object == null || reflectedClass.isInstance(object);
    130127                this.object = object;
     128                return this;
    131129        }
    132130
     
    159157        }
    160158
    161     @Override
    162     public Object createAccessee() {
    163         try {
    164             return reflectedClass.newInstance();
    165         } catch (InstantiationException e) {
    166             e.printStackTrace();
    167         } catch (IllegalAccessException e) {
    168             e.printStackTrace();
    169         }
    170         LOGGER.fatal("failed to create reflected object of class " + reflectedClass.getCanonicalName() + " for frams type " + framsClass.getId());
    171         return null;
    172     }
     159        @Override
     160        public Object createAccessee() {
     161                try {
     162                        return reflectedClass.newInstance();
     163                } catch (InstantiationException e) {
     164                        e.printStackTrace();
     165                } catch (IllegalAccessException e) {
     166                        e.printStackTrace();
     167                }
     168                log.fatal("failed to create reflected object of class " + reflectedClass.getCanonicalName() + " for frams type " + framsClass.getId());
     169                return null;
     170        }
    173171}
  • java/main/src/main/java/com/framsticks/params/Registry.java

    r78 r84  
    1010 */
    1111public class Registry {
    12         private static final Logger LOGGER = Logger.getLogger(Registry.class.getName());
     12        private static final Logger log = Logger.getLogger(Registry.class.getName());
    1313
    14         protected final Map<String, Class> reflectedClasses = new HashMap<String, Class>();
     14        protected final Map<String, Class<?>> reflectedClasses = new HashMap<String, Class<?>>();
    1515        protected final Map<String, String> invertedReflectedClasses = new HashMap<String, String>();
    1616        protected final Map<String, FramsClass> infoCache = new HashMap<String, FramsClass>();
     
    2020                        registerReflectedClass(name, id, Class.forName(className));
    2121                } catch (ClassNotFoundException e) {
    22                         LOGGER.fatal("class not found during registration: " + e);
     22                        log.fatal("class not found during registration: " + e);
    2323                }
    2424        }
    2525
    26         public void registerReflectedClass(String name, String id, Class reflectedClass) {
     26        public void registerReflectedClass(String name, String id,
     27                        Class<?> reflectedClass) {
    2728                if (name != null) {
    2829                        reflectedClasses.put(name, reflectedClass);
     
    4344        public void putInfoIntoCache(FramsClass framsClass) {
    4445                if (infoCache.containsKey(framsClass.getId())) {
    45                         LOGGER.info("already cached " + framsClass);
     46                        log.info("already cached " + framsClass);
    4647                        return;
    4748                }
    48                 LOGGER.debug("caching info for " + framsClass);
     49                log.debug("caching info for " + framsClass);
    4950                infoCache.put(framsClass.getId(), framsClass);
    5051                infoCache.put(framsClass.getName(), framsClass);
     
    6364                return null;
    6465        }
     66
     67        public static AccessInterface wrapAccessWithListIfNeeded(CompositeParam param, AccessInterface access) {
     68        if (access == null) {
     69                        return null;
     70                }
     71        return param.prepareAccessInterface(access);
     72        }
     73
     74    public AccessInterface prepareAccess(CompositeParam param) {
     75        return wrapAccessWithListIfNeeded(param, createAccess(param.getContainedTypeName()));
     76    }
     77
     78        public AccessInterface createAccess(String name) {
     79                if (name == null) {
     80                        return null;
     81                }
     82                FramsClass framsClass = getInfoFromCache(name);
     83                if (framsClass == null) {
     84                        return null;
     85                }
     86
     87                return createAccess(name, framsClass);
     88        }
     89
    6590}
  • java/main/src/main/java/com/framsticks/params/SimpleAbstractAccess.java

    r78 r84  
    22
    33import java.io.IOException;
    4 import java.text.DecimalFormat;
    5 import java.text.DecimalFormatSymbols;
    6 import java.util.ArrayList;
    74import java.util.Collection;
    8 import java.util.List;
    9 
    10 import com.framsticks.params.types.*;
     5
     6import static com.framsticks.util.lang.Containers.filterInstanceof;
     7
    118import org.apache.log4j.Logger;
    12 import java.util.Locale;
    139
    1410/**
     
    2622public abstract class SimpleAbstractAccess implements AccessInterface {
    2723
    28         private final static Logger LOGGER = Logger.getLogger(SimpleAbstractAccess.class.getName());
    29 
    30 
    31 
    32     @Override
     24        private final static Logger log = Logger.getLogger(SimpleAbstractAccess.class.getName());
     25
     26        @Override
    3327        public final FramsClass getFramsClass() {
    3428                return framsClass;
     
    3832                this.framsClass = framsClass;
    3933        }
    40     /**
     34        /**
    4135         * Simple String key, value class.
    4236         */
     
    6458        }
    6559
    66     @Override
     60        @Override
    6761        public int getParamCount() {
    6862                return framsClass.getParamCount();
     
    7165        @Override
    7266        public Param getParam(int i) {
    73                 return framsClass.getParamEntry(i);
     67                return framsClass.getParamEntry(i, Param.class);
    7468        }
    7569
    7670        @Override
    7771        public Param getParam(String id) {
    78                 return framsClass.getParamEntry(id);
    79         }
    80 
     72                return framsClass.getParamEntry(id, Param.class);
     73        }
    8174
    8275        @Override
     
    8780        @Override
    8881        public <T> T get(int i, Class<T> type) {
    89                 return get(getParam(i), type);
     82                Param p = getParam(i);
     83                assert p instanceof ValueParam;
     84                return get((ValueParam) p, type);
    9085        }
    9186
    9287        @Override
    9388        public <T> T get(String id, Class<T> type) {
    94                 return get(getParam(id), type);
    95         }
    96 
     89                Param p = getParam(id);
     90                assert p instanceof ValueParam;
     91                return get((ValueParam) p, type);
     92        }
    9793
    9894        @Override
    9995        public <T> int set(String id, T value) {
    100                 return set(getParam(id), value);
     96                Param p = getParam(id);
     97                assert p instanceof ValueParam;
     98                return set((ValueParam) p, value);
    10199        }
    102100
    103101        @Override
    104102        public <T> int set(int i, T value) {
    105                 return set(getParam(i), value);
    106         }
    107 
    108         @SuppressWarnings("unchecked")
    109         @Override
    110         public <T> int set(Param param, T value) {
     103                Param p = getParam(i);
     104                assert p instanceof ValueParam;
     105                return set((ValueParam) p, value);
     106        }
     107
     108        @Override
     109        public <T> int set(ValueParam param, T value) {
    111110                int flags = 0;
     111
    112112                //String id = param.getEffectiveId();
    113113                try {
    114             Object oldValue = get(param, param.getStorageType());
    115             Object casted = param.reassign(value, oldValue);
    116             if (casted != oldValue) {
    117                 internalSet(param, casted);
    118             }
    119         } catch (CastFailure e) {
    120             LOGGER.error("casting failure while set: ", e);
    121 
    122         }
    123         return flags;
    124     }
    125 
    126 
    127 
     114                        Object oldValue = get(param, param.getStorageType());
     115                        ReassignResult<?> result = param.reassign(value, oldValue);
     116                        Object casted = result.getValue();
     117                        if (casted != oldValue) {
     118                                internalSet(param, casted);
     119                        }
     120                        flags = result.getFlags();
     121                } catch (CastFailure e) {
     122                        log.error("casting failure while set: ", e);
     123                }
     124                return flags;
     125        }
    128126
    129127        @Override
    130128        public void setDefault(boolean numericOnly) {
    131                 for (int i = 0; i < framsClass.getParamCount(); i++)
     129                for (int i = 0; i < framsClass.getParamCount(); i++) {
    132130                        setDefault(i, numericOnly);
     131                }
    133132        }
    134133
    135134        @Override
    136135        public void setDefault(int i, boolean numericOnly) {
    137                 Param entry = framsClass.getParamEntry(i);
     136                ValueParam entry = framsClass.getParamEntry(i, ValueParam.class);
    138137                if ((entry != null)     && (!numericOnly || entry.isNumeric())) {
    139138                        set(i, entry.getDef(entry.getStorageType()));
     
    148147        }
    149148
    150         @SuppressWarnings("unchecked")
    151149        @Override
    152150        public void setMin(int i) {
    153                 Param entry = framsClass.getParamEntry(i);
     151                PrimitiveParam entry = framsClass.getParamEntry(i, PrimitiveParam.class);
     152                if (entry == null) {
     153                        return;
     154                }
    154155                Object min = entry.getMin(entry.getStorageType());
    155156                if (min != null) {
     
    165166        }
    166167
    167         @SuppressWarnings("unchecked")
    168168        @Override
    169169        public void setMax(int i) {
    170                 Param entry = framsClass.getParamEntry(i);
     170                PrimitiveParam entry = framsClass.getParamEntry(i, PrimitiveParam.class);
     171                if (entry == null) {
     172                        return;
     173                }
    171174                Object max = entry.getMax(entry.getStorageType());
    172175                if (max != null) {
     
    191194        @Override
    192195        public void save(SinkInterface sink) {
    193         assert framsClass != null;
    194         sink.print(framsClass.getId()).print(":").breakLine();
    195         for (Param p : framsClass.getParamEntries()) {
    196             if (p instanceof ValueParam) {
    197                 Object value = get(p, Object.class);
    198                 if (value == null) {
    199                     continue;
    200                 }
    201                 sink.print(p.getId()).print(":");
    202                 ((ValueParam)p).save(sink, value);
    203                 sink.breakLine();
    204 
    205 
    206                 //continue;
    207             }
    208             /*
    209             if (p instanceof CompositeParam) {
    210                 if (!(p instanceof ListParam)) {
    211                     continue;
    212                 }
    213                 Collection c = get(p, Collection.class);
    214                 if (c != null) {
    215                     save(stream, p, c.size());
    216                 }
    217             }
    218             */
    219 
    220         }
    221         sink.breakLine();
    222         }
    223 
    224         @Override
    225         public String save2(boolean omitDefaultValues) {
    226                 StringBuilder stringBuilder = new StringBuilder();
    227                 DecimalFormat df = new DecimalFormat("#.###", new DecimalFormatSymbols(
    228                                 Locale.ENGLISH));
    229                 boolean canOmitName = false;
    230                 int n = 0;
    231                 for (int i = 0; i < framsClass.getParamCount(); i++) {
    232                         Object value = this.get(i, Object.class);
    233                         Object def = framsClass.getParamEntry(i).getDef(Object.class);
    234                         if (value != null && (!omitDefaultValues || !value.equals(def))) {
    235                                 if ((n != i && !canOmitName)
    236                                                 || (getParam(i).getFlags() & Flags.CANOMITNAME) == 0) {
    237                                         stringBuilder.append(getParam(i).getId());
    238                                         stringBuilder.append("=");
     196                assert framsClass != null;
     197                sink.print(framsClass.getId()).print(":").breakLine();
     198                for (PrimitiveParam p : filterInstanceof(framsClass.getParamEntries(), PrimitiveParam.class)) {
     199                        Object value = get(p, Object.class);
     200                        if (value == null) {
     201                                continue;
     202                        }
     203                        sink.print(p.getId()).print(":");
     204                        p.save(sink, value);
     205                        sink.breakLine();
     206                }
     207                sink.breakLine();
     208        }
     209
     210        private Entry readEntry(SourceInterface source)
     211                        throws IOException {
     212
     213                String line;
     214                String key = null;
     215                StringBuilder value = null;
     216                while ((line = source.readLine()) != null)
     217                {
     218                        if (key == null) {
     219                                int colonIndex = line.indexOf(':');
     220                                if (colonIndex == -1) {
     221                                        return null;
    239222                                }
    240                                 n++;
    241                                 canOmitName = true;
    242                                 if (this.get(i, Object.class) instanceof String) {
    243                                         String valueString = this.get(i, String.class);
    244                                         valueString = valueString.replaceAll("\\\"", "\\\\\"");
    245                                         valueString = valueString.contains(",") ? "\""
    246                                                         + valueString + "\"" : valueString;
    247                                         // if ("".equals(valueString.trim()))
    248                                         // value = "\"\"";
    249                                         stringBuilder.append(valueString);
    250                                 } else if (this.get(i, Object.class) instanceof Double) {
    251                                         stringBuilder.append(df.format(this.get(i, Double.class)));
    252                                 } else
    253                                         stringBuilder.append(this.get(i, Object.class));
    254                                 stringBuilder.append(", ");
     223                                key = line.substring(0, colonIndex);
     224                                String inlineValue = line.substring(colonIndex + 1);
     225
     226
     227                                if (!inlineValue.startsWith("~")) {
     228                                        return new Entry(key, inlineValue);
     229                                }
     230                                value = new StringBuilder();
     231                                value.append(inlineValue.substring(1));
     232                                continue;
     233                        }
     234                        if (value.length() != 0) {
     235                                value.append(System.getProperty("line.separator"));
     236                        }
     237                        if (line.contains("~")) {
     238                                value.append(line.substring(0, line.indexOf("~")));
     239                                return new Entry(key, value.toString());
     240                        }
     241                        value.append(line);
     242                        /*
     243                        if (line.contains("~")) {
     244                                String lastLine = line.substring(0, line.indexOf("~"));
     245                                if (lastLine.length() > 0) {
     246                                        appendToValue(value, lastLine);
     247                                }
     248                                return new Entry(key, value.toString());
     249                        }
     250                        appendToValue(value, line);
     251                        */
     252                }
     253                return null;
     254        }
     255
     256        @Override
     257        public void load(SourceInterface source) throws Exception {
     258                //TODO not clearing values, because get from manager gives only fields, not children
     259                //this.clearValues();
     260
     261                Entry entry;
     262                while ((entry = readEntry(source)) != null) {
     263                        Param param = getParam(entry.key);
     264                        if (param == null) {
     265                                continue;
     266                        }
     267                        if (!(param instanceof ValueParam)) {
     268                                log.warn("param " + param + " is not a ValueParam");
     269                                continue;
     270                        }
     271                        if ((param.getFlags() & Flags.DONTLOAD) != 0) {
     272                                log.debug("DontLoad flag was set - not loading...");
    255273                        } else {
    256                                 canOmitName = false;
    257                         }
    258                 }
    259 
    260                 String params = "";
    261                 if (stringBuilder.length() > 1)
    262                         params = stringBuilder.substring(0, stringBuilder.length() - 2);
    263 
    264                 return getId() + ":" + params;
    265         }
    266 
    267     /*
    268     private static void appendToValue(StringBuilder value, String line) {
    269 
    270         if (value.length() != 0) {
    271             value.append(System.getProperty("line.separator"));
    272         }
    273         value.append(line);
    274     }
    275     */
    276 
    277     private Entry readEntry(SourceInterface source)
    278             throws IOException {
    279 
    280         String line;
    281         String key = null;
    282         StringBuilder value = null;
    283         while ((line = source.readLine()) != null)
    284         {
    285             if (key == null) {
    286                 int colonIndex = line.indexOf(':');
    287                 if (colonIndex == -1) {
    288                     return null;
    289                 }
    290                 key = line.substring(0, colonIndex);
    291                 String inlineValue = line.substring(colonIndex + 1);
    292 
    293 
    294                 if (!inlineValue.startsWith("~")) {
    295                     return new Entry(key, inlineValue);
    296                 }
    297                 value = new StringBuilder();
    298                 value.append(inlineValue.substring(1));
    299                 continue;
    300             }
    301             if (value.length() != 0) {
    302                 value.append(System.getProperty("line.separator"));
    303             }
    304             if (line.contains("~")) {
    305                 value.append(line.substring(0, line.indexOf("~")));
    306                 return new Entry(key, value.toString());
    307             }
    308             value.append(line);
    309             /*
    310             if (line.contains("~")) {
    311                 String lastLine = line.substring(0, line.indexOf("~"));
    312                 if (lastLine.length() > 0) {
    313                     appendToValue(value, lastLine);
    314                 }
    315                 return new Entry(key, value.toString());
    316             }
    317             appendToValue(value, line);
    318             */
    319         }
    320         return null;
    321     }
    322 
    323     @Override
    324     public void load(SourceInterface source) throws Exception {
    325         //TODO not clearing values, because get from manager gives only fields, not children
    326         //this.clearValues();
    327 
    328         Entry entry;
    329         while ((entry = readEntry(source)) != null) {
    330             Param param = getParam(entry.key);
    331             if (param == null) {
    332                 continue;
    333             }
    334             if ((param.getFlags() & Flags.DONTLOAD) != 0) {
    335                 LOGGER.debug("DontLoad flag was set - not loading...");
    336             } else {
    337                 int retFlags = this.set(param, entry.value);
    338                 if ((retFlags & (Flags.PSET_HITMIN | Flags.PSET_HITMAX)) != 0) {
    339                     String which = ((retFlags & Flags.PSET_HITMIN) != 0) ? "small"
    340                             : "big";
    341                     LOGGER.warn("value of key '" + entry.key
    342                             + "' was too " + which + ", adjusted");
    343                 }
    344             }
    345         }
    346     }
    347 
    348 
    349 
    350         protected abstract <T> void internalSet(Param param, T value);
    351 
    352     @Override
    353     public Collection<Param> getParams() {
    354         return framsClass.getParamEntries();
    355     }
    356 
    357     /*
     274                                int retFlags = this.set((ValueParam) param, entry.value);
     275                                if ((retFlags & (Flags.PSET_HITMIN | Flags.PSET_HITMAX)) != 0) {
     276                                        String which = ((retFlags & Flags.PSET_HITMIN) != 0) ? "small" : "big";
     277                                        log.warn("value of key '" + entry.key + "' was too " + which + ", adjusted");
     278                                }
     279                        }
     280                }
     281        }
     282
     283        protected abstract <T> void internalSet(ValueParam param, T value);
     284
     285        @Override
     286        public Collection<Param> getParams() {
     287                return framsClass.getParamEntries();
     288        }
     289
     290        /*
    358291        protected <T extends Comparable<T>> int setAndCut(Param param, Object value, Class<T> type) {
    359292                int flags = 0;
  • java/main/src/main/java/com/framsticks/params/UniqueListAccess.java

    r77 r84  
    11package com.framsticks.params;
    22
    3 import com.framsticks.util.Casting;
    4 import com.framsticks.util.Numbers;
     3import com.framsticks.util.lang.Casting;
     4import com.framsticks.util.lang.Numbers;
    55import org.apache.log4j.Logger;
    66
     
    1212public class UniqueListAccess extends ListAccess {
    1313
    14     private static final Logger LOGGER = Logger.getLogger(UniqueListAccess.class.getName());
     14        private static final Logger log = Logger.getLogger(UniqueListAccess.class.getName());
    1515
    16     Map<String, Object> map;
     16        Map<String, Object> map;
    1717
    18     final String uidName;
     18        final String uidName;
    1919
    20     public UniqueListAccess(AccessInterface elementAccess, String uidName) {
    21         super(elementAccess);
    22         this.uidName = uidName;
    23     }
     20        public UniqueListAccess(AccessInterface elementAccess, String uidName) {
     21                super(elementAccess);
     22                this.uidName = uidName;
     23        }
    2424
    25     @Override
    26     public Map<String, Object> createAccessee() {
    27         return new HashMap<String, Object>();
    28     }
     25        @Override
     26        public Map<String, Object> createAccessee() {
     27                return new HashMap<String, Object>();
     28        }
    2929
    30     @Override
    31     public Param getParam(int i) {
    32         LOGGER.error("accesing unique list through index");
    33         assert false;
    34         return null;
    35         //LOGGER.error("accesing unique list through index");
    36         //return null;
    37         //return new ParamBuilder().setId(Integer.toString(i)).setName(elementAccess.getId()).setType("o " + elementAccess.getId()).build();
    38     }
     30        @Override
     31        public Param getParam(int i) {
     32                log.error("accesing unique list through index");
     33                assert false;
     34                return null;
     35                //log.error("accesing unique list through index");
     36                //return null;
     37                //return new ParamBuilder().setId(Integer.toString(i)).setName(elementAccess.getId()).setType("o " + elementAccess.getId()).build();
     38        }
    3939
    40     @Override
    41     public Param getParam(String id) {
    42         Integer i = Numbers.parse(id, Integer.class);
    43         if (i != null) {
    44             return getParam(i);
    45         }
    46         return new ParamBuilder().setId(id).setName(elementAccess.getId()).setType("o " + elementAccess.getId()).build();
    47     }
     40        @Override
     41        public Param getParam(String id) {
     42                Integer i = Numbers.parse(id, Integer.class);
     43                if (i != null) {
     44                        return getParam(i);
     45                }
     46                return new ParamBuilder().setId(id).setName(elementAccess.getId()).setType("o " + elementAccess.getId()).build();
     47        }
    4848
    49     @Override
    50     public String getId() {
    51         return "l " + elementAccess.getId() + " " + uidName;
    52     }
     49        @Override
     50        public String getId() {
     51                return "l " + elementAccess.getId() + " " + uidName;
     52        }
    5353
    54     @Override
    55     public int getParamCount() {
    56         return map.size();
    57     }
     54        @Override
     55        public int getParamCount() {
     56                return map.size();
     57        }
    5858
    59     @Override
    60     public <T> T get(int i, Class<T> type) {
    61         LOGGER.error("accesing unique list through index");
    62         assert false;
    63         return null;
    64         //Object[] values = map.values().toArray();
    65         //return Casting.tryCast(i < values.length ? values[i] : null, type);
    66     }
     59        @Override
     60        public <T> T get(int i, Class<T> type) {
     61                log.error("accesing unique list through index");
     62                assert false;
     63                return null;
     64                //Object[] values = map.values().toArray();
     65                //return Casting.tryCast(i < values.length ? values[i] : null, type);
     66        }
    6767
    68     @Override
    69     public <T> T get(String id, Class<T> type) {
    70         Integer i = Numbers.parse(id, Integer.class);
    71         if (i != null) {
    72             return get(i, type);
    73         }
    74         return Casting.tryCast(type, map.containsKey(id) ? map.get(id) : null);
    75     }
     68        @Override
     69        public <T> T get(String id, Class<T> type) {
     70                Integer i = Numbers.parse(id, Integer.class);
     71                if (i != null) {
     72                        return get(i, type);
     73                }
     74                return Casting.tryCast(type, map.containsKey(id) ? map.get(id) : null);
     75        }
    7676
    77     @Override
    78     public <T> T get(Param param, Class<T> type) {
    79         return get(param.getId(), type);
    80     }
     77        @Override
     78        public <T> T get(ValueParam param, Class<T> type) {
     79                return get(param.getId(), type);
     80        }
    8181
    82     public String getUidOf(Object value) {
    83         Object tmp = elementAccess.getSelected();
    84         elementAccess.select(value);
    85         String uid = elementAccess.get(uidName, String.class);
    86         elementAccess.select(tmp);
    87         if (uid == null) {
    88             return null;
    89         }
    90         return uid;
    91     }
     82        public String getUidOf(Object value) {
     83                Object tmp = elementAccess.getSelected();
     84                elementAccess.select(value);
     85                String uid = elementAccess.get(uidName, String.class);
     86                elementAccess.select(tmp);
     87                if (uid == null) {
     88                        return null;
     89                }
     90                return uid;
     91        }
    9292
    93     protected int setByUid(Object object, String uid) {
    94         if (uid == null) {
    95             uid = getUidOf(object);
    96             if (uid == null) {
    97                 LOGGER.error("failed to set - missing uid");
    98                 return 0;
    99             }
    100         }
    101         if (object == null) {
    102             map.remove(uid);
    103         } else {
    104             map.put(uid, object);
    105         }
    106         return 0;
    107     }
     93        protected int setByUid(Object object, String uid) {
     94                if (uid == null) {
     95                        uid = getUidOf(object);
     96                        if (uid == null) {
     97                                log.error("failed to set - missing uid");
     98                                return 0;
     99                        }
     100                }
     101                if (object == null) {
     102                        map.remove(uid);
     103                } else {
     104                        map.put(uid, object);
     105                }
     106                return 0;
     107        }
    108108
    109     @Override
    110     public <T> int set(int i, T value) {
    111         LOGGER.error("accesing unique list through index");
    112         //return setByUid(value, null);
    113         return 0;
    114     }
     109        @Override
     110        public <T> int set(int i, T value) {
     111                log.error("accesing unique list through index");
     112                //return setByUid(value, null);
     113                return 0;
     114        }
    115115
    116     @Override
    117     public <T> int set(String id, T value) {
    118         Integer i = Numbers.parse(id, Integer.class);
    119         if (i != null) {
    120             return set(i, value);
    121         }
    122         if (value == null) {
    123             return setByUid(null, id);
    124         }
    125         String uid = getUidOf(value);
    126         if (uid != null && id != null) {
    127             if (!id.equals(uid)) {
    128                 LOGGER.error("uid mismatch with set key");
    129                 return 0;
    130             }
    131             setByUid(value, uid);
    132             return 0;
    133         }
    134         if (uid != null) {
    135             setByUid(value, uid);
    136             return 0;
    137         }
    138         if (id != null) {
    139             setByUid(value, id);
    140             return 0;
    141         }
    142         LOGGER.error("missing both uid and id - failed to set");
    143         return 0;
    144     }
     116        @Override
     117        public <T> int set(String id, T value) {
     118                Integer i = Numbers.parse(id, Integer.class);
     119                if (i != null) {
     120                        return set(i, value);
     121                }
     122                if (value == null) {
     123                        return setByUid(null, id);
     124                }
     125                String uid = getUidOf(value);
     126                if (uid != null && id != null) {
     127                        if (!id.equals(uid)) {
     128                                log.error("uid mismatch with set key");
     129                                return 0;
     130                        }
     131                        setByUid(value, uid);
     132                        return 0;
     133                }
     134                if (uid != null) {
     135                        setByUid(value, uid);
     136                        return 0;
     137                }
     138                if (id != null) {
     139                        setByUid(value, id);
     140                        return 0;
     141                }
     142                log.error("missing both uid and id - failed to set");
     143                return 0;
     144        }
    145145
    146     @Override
    147     public <T> int set(Param param, T value) {
    148         return set(param.getId(), value);
    149     }
     146        @Override
     147        public <T> int set(ValueParam param, T value) {
     148                return set(param.getId(), value);
     149        }
    150150
    151     @Override
    152     public void clearValues() {
    153         map.clear();
    154     }
     151        @Override
     152        public void clearValues() {
     153                map.clear();
     154        }
    155155
    156     @Override
    157     public void select(Object object) {
    158         map = (Map<String, Object>)object;
    159     }
     156        @SuppressWarnings("unchecked")
     157        @Override
     158        public UniqueListAccess select(Object object) {
     159                assert (object instanceof Map);
     160                map = (Map<String, Object>) object;
     161                return this;
     162        }
    160163
    161     @Override
    162     public Object getSelected() {
    163         return map;
    164     }
     164        @Override
     165        public Object getSelected() {
     166                return map;
     167        }
    165168
    166     @Override
    167     public UniqueListAccess cloneAccess() {
    168         return new UniqueListAccess(elementAccess.cloneAccess(), uidName);
    169     }
     169        @Override
     170        public UniqueListAccess cloneAccess() {
     171                return new UniqueListAccess(elementAccess.cloneAccess(), uidName);
     172        }
    170173
    171     @Override
    172     public String computeIdentifierFor(Object selected) {
    173         String uid = getUidOf(selected);
    174         if (uid == null) {
    175             LOGGER.error("missing uid field");
    176             return null;
    177         }
    178         return uid;
    179     }
     174        @Override
     175        public String computeIdentifierFor(Object selected) {
     176                String uid = getUidOf(selected);
     177                if (uid == null) {
     178                        log.error("missing uid field");
     179                        return null;
     180                }
     181                return uid;
     182        }
    180183
    181     @Override
    182     public Collection<Param> getParams() {
    183         List<Param> result = new LinkedList<Param>();
    184         if (map == null) {
    185             return result;
    186         }
    187         for (String k : map.keySet()) {
    188             result.add(getParam(k));
    189         }
    190         return result;
    191     }
     184        @Override
     185        public Collection<Param> getParams() {
     186                List<Param> result = new LinkedList<Param>();
     187                if (map == null) {
     188                        return result;
     189                }
     190                for (String k : map.keySet()) {
     191                        result.add(getParam(k));
     192                }
     193                return result;
     194        }
    192195}
  • java/main/src/main/java/com/framsticks/params/Util.java

    r78 r84  
    44import java.util.List;
    55
     6import static com.framsticks.util.lang.Containers.filterInstanceof;
     7
    68/**
    79 * @author Piotr Sniegowski
    810 */
    911public abstract class Util {
    10     public static String readSourceToString(SourceInterface source) {
    11         StringBuilder result = new StringBuilder();
    12         String line;
    13         while ((line = source.readLine()) != null) {
    14             result.append(line).append(" ");
    15         }
    16         source.close();
    17         return result.toString();
    18     }
     12
     13        public static String readSourceToString(SourceInterface source) {
     14                StringBuilder result = new StringBuilder();
     15                String line;
     16                while ((line = source.readLine()) != null) {
     17                        result.append(line).append(" ");
     18                }
     19                source.close();
     20                return result.toString();
     21        }
     22
    1923        public static List<Object> stripAccessInterface(List<AccessInterface> accesses) {
    2024                List<Object> result = new ArrayList<Object>();
     
    2428                return result;
    2529        }
     30
    2631        public static int copyParams(AccessInterface to, AccessInterface from) {
    2732                int copied = 0;
    28                 for (Param f : from.getParams()) {
     33                for (ValueParam f : filterInstanceof(from.getParams(), ValueParam.class)) {
    2934                        Param t = from.getParam(f.getId());
    30                         if (t == null) {
     35                        if (!(t instanceof ValueParam)) {
    3136                                continue;
    3237                        }
     
    3439                                continue;
    3540                        }
    36                         to.set(t, from.get(f, Object.class));
     41                        to.set((ValueParam) t, from.get(f, Object.class));
    3742                        ++copied;
    3843                }
  • java/main/src/main/java/com/framsticks/params/types/ArrayListParam.java

    r77 r84  
    44import com.framsticks.params.ArrayListAccess;
    55import com.framsticks.params.CastFailure;
    6 import com.framsticks.util.Casting;
    7 import com.framsticks.util.Containers;
    8 import com.framsticks.util.Numbers;
     6import com.framsticks.params.ReassignResult;
     7import com.framsticks.util.lang.Casting;
     8import com.framsticks.util.lang.Containers;
     9import com.framsticks.util.lang.Numbers;
    910
    1011import java.util.ArrayList;
     
    1617public class ArrayListParam extends ListParam {
    1718
    18     public ArrayListParam(String containedTypeName) {
    19         super(containedTypeName);
    20     }
     19        public ArrayListParam(String containedTypeName) {
     20                super(containedTypeName);
     21        }
    2122
    2223
    23     @Override
    24     public Class getStorageType() {
    25         return List.class;
    26     }
     24        @Override
     25        public Class<?> getStorageType() {
     26                return List.class;
     27        }
    2728
    28     @Override
    29     public AccessInterface prepareAccessInterface(AccessInterface access) {
    30         return new ArrayListAccess(access);
    31     }
     29        @Override
     30        public AccessInterface prepareAccessInterface(AccessInterface access) {
     31                return new ArrayListAccess(access);
     32        }
    3233
    33     @Override
    34     public String computeAccessId() {
    35         return "l " + containedTypeName;
    36     }
     34        @Override
     35        public String computeAccessId() {
     36                return "l " + containedTypeName;
     37        }
    3738
    38     @Override
    39     public Object reassign(Object newValue, Object oldValue) throws CastFailure {
    40         if (newValue == null) {
    41             throw new CastFailure();
    42         }
    43         Integer size = Numbers.cast(newValue, Integer.class);
    44         if (size != null) {
    45             //return oldValue;
    46             List list;
    47             if (oldValue == null) {
    48                 list = new ArrayList();
    49             } else {
    50                 list = Casting.tryCast(List.class, oldValue);
    51                 if (list == null) {
    52                     throw new CastFailure();
    53                 }
    54             }
    55             Containers.resizeList(list, size);
    56             return list;
    57         }
    58         if (oldValue != null) {
    59             return oldValue;
    60         }
    61         return newValue;
    62     }
     39        @Override
     40        public ReassignResult<List<?>> reassign(Object newValue, Object oldValue) throws CastFailure {
     41                if (newValue == null) {
     42                        throw new CastFailure();
     43                }
     44                Integer size = Numbers.cast(newValue, Integer.class);
     45                if (size != null) {
     46                        //return oldValue;
     47                        List<?> list;
     48                        if (oldValue == null) {
     49                                list = new ArrayList<Object>();
     50                        } else {
     51                                list = Casting.tryCast(List.class, oldValue);
     52                                if (list == null) {
     53                                        throw new CastFailure();
     54                                }
     55                        }
     56                        Containers.resizeList(list, size);
     57                        return new ReassignResult<List<?>>(list);
     58                }
     59                if (oldValue != null) {
     60                        return new ReassignResult<List<?>>(Casting.throwCast(List.class, oldValue));
     61                }
     62                return new ReassignResult<List<?>>(Casting.throwCast(List.class, newValue));
     63        }
    6364
    64     @Override
    65     public String getType() {
    66         return "l " + getContainedTypeName();
    67     }
     65        @Override
     66        public String getFramsTypeName() {
     67                return "l " + getContainedTypeName();
     68        }
    6869}
  • java/main/src/main/java/com/framsticks/params/types/BinaryParam.java

    r77 r84  
    22
    33import com.framsticks.params.CastFailure;
    4 import com.framsticks.util.Numbers;
     4import com.framsticks.params.PrimitiveParam;
     5import com.framsticks.params.ReassignResult;
     6import com.framsticks.util.lang.Numbers;
    57
    68/**
    79 * @author Piotr Sniegowski
    810 */
    9 public class BinaryParam extends ValueParam {
     11public class BinaryParam extends PrimitiveParam {
     12
    1013        @Override
    11         public Class getStorageType() {
     14        public Class<?> getStorageType() {
    1215                return Integer.class;
    1316        }
    1417
    15     @Override
    16     public String getType() {
    17         return "db";
    18     }
     18        @Override
     19        public String getFramsTypeName() {
     20                return "db";
     21        }
    1922
    20     @Override
    21     public Object reassign(Object newValue, Object oldValue) throws CastFailure {
    22         if (newValue instanceof String) {
    23             Integer v = Numbers.parse((String) newValue, Integer.class);
    24             if (v != null) {
    25                 return v;
    26             }
    27             throw new CastFailure();
    28         }
    29         return super.reassign(newValue, oldValue);
    30     }
     23        @Override
     24        public ReassignResult<?> reassign(Object newValue, Object oldValue) throws CastFailure {
     25                if (newValue instanceof String) {
     26                        Integer v = Numbers.parse((String) newValue, Integer.class);
     27                        if (v != null) {
     28                                return ReassignResult.create(v);
     29                        }
     30                        throw new CastFailure();
     31                }
     32                if (newValue instanceof Integer) {
     33                        return ReassignResult.create((Integer) newValue);
     34                }
     35                throw new CastFailure();
     36        }
    3137
    3238}
  • java/main/src/main/java/com/framsticks/params/types/BooleanParam.java

    r77 r84  
    22
    33import com.framsticks.params.CastFailure;
     4import com.framsticks.params.PrimitiveParam;
     5import com.framsticks.params.ReassignResult;
    46import com.framsticks.params.SinkInterface;
     7import com.framsticks.util.lang.Numbers;
    58
    69/**
    710 * @author Piotr Sniegowski
    811 */
    9 public class BooleanParam extends DecimalParam {
    10     @Override
    11     public Class getStorageType() {
    12         return Boolean.class;
    13     }
     12public class BooleanParam extends PrimitiveParam {
    1413
    15     @Override
    16     public Object reassign(Object newValue, Object oldValue) throws CastFailure {
     14        public BooleanParam() {
     15                def = false;
     16        }
     17
     18        @Override
     19        public Class<?> getStorageType() {
     20                return Boolean.class;
     21        }
     22
     23        @Override
     24        public ReassignResult<Boolean> reassign(Object newValue, Object oldValue) throws CastFailure {
    1725                if (newValue instanceof Boolean) {
    18                         return newValue;
     26                        return ReassignResult.create((Boolean) newValue);
    1927                }
    20         Object casted = super.reassign(newValue, oldValue);
    21         if (casted instanceof Integer) {
    22             Integer i = (Integer)casted;
    23             if (i == 0) return false;
    24             if (i == 1) return true;
    25             throw new CastFailure();
    26         }
    27         /*
    28         if (newValue instanceof String) {
    29             if (newValue.equals("true")) return true;
    30             if (newValue.equals("false")) return false;
    31             throw new CastFailure();
    32         }
    33         */
    34         throw new CastFailure();
    35     }
     28                if (newValue instanceof Integer) {
     29                        return ReassignResult.create(((Integer) newValue) != 0);
     30                }
     31                if (newValue instanceof String) {
     32                        if ("true".equals(newValue)) {
     33                                return ReassignResult.create(true);
     34                        }
     35                        if ("false".equals(newValue)) {
     36                                return ReassignResult.create(false);
     37                        }
     38                        Integer i = Numbers.cast(newValue, Integer.class);
     39                        if (i != null) {
     40                                return ReassignResult.create(i != 0);
     41                        }
     42                        throw new CastFailure();
     43                }
     44                throw new CastFailure();
     45        }
    3646
    37     @Override
    38     public String getType() {
    39         return "d 0 1";
    40     }
     47        @Override
     48        public String getFramsTypeName() {
     49                return "d 0 1";
     50        }
    4151
    42     @Override
    43     public void save(SinkInterface sink, Object value) {
    44         assert value instanceof Boolean;
    45         sink.print(((Boolean)value) ? "1" : "0");
    46     }
     52        @Override
     53        public void save(SinkInterface sink, Object value) {
     54                assert value instanceof Boolean;
     55                sink.print(((Boolean)value) ? "1" : "0");
     56        }
    4757}
  • java/main/src/main/java/com/framsticks/params/types/ColorParam.java

    r77 r84  
    11package com.framsticks.params.types;
     2
     3import com.framsticks.params.CastFailure;
     4import com.framsticks.params.PrimitiveParam;
     5import com.framsticks.params.ReassignResult;
    26
    37/**
    48 * @author Piotr Sniegowski
    59 */
    6 public class ColorParam extends ValueParam {
     10public class ColorParam extends PrimitiveParam {
    711
    812        @Override
    9         public Class getStorageType() {
    10                 return Void.class;
     13        public Class<?> getStorageType() {
     14                return Object.class;
    1115        }
    1216
    13     @Override
    14     public String getType() {
    15         return "dc";
    16     }
     17        @Override
     18        public String getFramsTypeName() {
     19                return "dc";
     20        }
    1721
     22        public ReassignResult<Object> reassign(Object newValue, Object oldValue) throws CastFailure {
     23                return ReassignResult.create(newValue);
     24        }
    1825
    1926
  • java/main/src/main/java/com/framsticks/params/types/DecimalParam.java

    r77 r84  
    22
    33import com.framsticks.params.CastFailure;
    4 import com.framsticks.util.Numbers;
     4import com.framsticks.params.ReassignResult;
    55
    66/**
    77 * @author Piotr Sniegowski
    88 */
    9 public class DecimalParam extends ValueParam {
     9public class DecimalParam extends NumberParam<Integer> {
    1010
    11         @Override
    12         public boolean isNumeric() {
    13                 return true;
     11        public DecimalParam() {
     12                def = 0;
    1413        }
    1514
    1615        @Override
    17         public Class getStorageType() {
     16        public Class<?> getStorageType() {
    1817                return Integer.class;
    1918        }
    2019
     20        @Override
     21        public ReassignResult<Integer> reassign(Object newValue, Object oldValue) throws CastFailure {
     22                return reassignNumber(newValue, oldValue, Integer.class);
     23        }
    2124
    22 
    23     @Override
    24     public Object reassign(Object newValue, Object oldValue) throws CastFailure {
    25         if (newValue instanceof String) {
    26             Integer v = Numbers.parse((String)newValue, Integer.class);
    27             if (v != null) {
    28                 return v;
    29             }
    30             throw new CastFailure();
    31         }
    32         return super.reassign(newValue, oldValue);
    33     }
    34 
    35     @Override
    36     public String getType() {
    37         return "d";
    38     }
     25        @Override
     26        public String getFramsTypeName() {
     27                return "d";
     28        }
    3929
    4030
  • java/main/src/main/java/com/framsticks/params/types/EnumParam.java

    r77 r84  
    11package com.framsticks.params.types;
    22
    3 import java.util.ArrayList;
    43import java.util.List;
    54
     
    98public class EnumParam extends DecimalParam {
    109
    11         ArrayList<String> enums;
     10        List<String> enums;
    1211
    1312        /**
     
    1615         * @param enums
    1716         */
    18         public EnumParam(ArrayList<String> enums) {
     17        public EnumParam(List<String> enums) {
    1918                assert(enums != null);
    2019                this.enums = enums;
  • java/main/src/main/java/com/framsticks/params/types/EventParam.java

    r77 r84  
    77 */
    88public class EventParam extends Param {
     9
    910        @Override
    10         public Class getStorageType() {
     11        public Class<?> getStorageType() {
    1112                return Void.class;
    1213        }
    1314
    14     @Override
    15     public String getType() {
    16         return "e";
    17     }
    18 
    19 
    20 
     15        @Override
     16        public String getFramsTypeName() {
     17                return "e";
     18        }
    2119}
  • java/main/src/main/java/com/framsticks/params/types/FloatParam.java

    r77 r84  
    22
    33import com.framsticks.params.CastFailure;
    4 import com.framsticks.util.Numbers;
     4import com.framsticks.params.ReassignResult;
    55
    66/**
    77 * @author Piotr Sniegowski
    88 */
    9 public class FloatParam extends ValueParam {
     9public class FloatParam extends NumberParam<Double> {
     10
     11        public FloatParam() {
     12                def = 0.0;
     13        }
     14
    1015        @Override
    11         public boolean isNumeric() {
    12                 return true;
    13         }
    14         @Override
    15         public Class getStorageType() {
     16        public Class<?> getStorageType() {
    1617                return Double.class;
    1718        }
    1819
    1920        @Override
    20     public Object reassign(Object newValue, Object oldValue) throws CastFailure {
    21         if (newValue instanceof String) {
    22                     Double v = Numbers.parse((String)newValue, Double.class);
    23             if (v != null) {
    24                 return v;
    25             }
    26             throw new CastFailure();
    27         }
    28         return super.reassign(newValue, oldValue);
     21        public ReassignResult<Double> reassign(Object newValue, Object oldValue) throws CastFailure {
     22                return reassignNumber(newValue, oldValue, Double.class);
    2923        }
    3024
    31     @Override
    32     public String getType() {
    33         return "f";
    34     }
     25        @Override
     26        public String getFramsTypeName() {
     27                return "f";
     28        }
    3529
    3630
  • java/main/src/main/java/com/framsticks/params/types/ListParam.java

    r77 r84  
    11package com.framsticks.params.types;
    22
    3 import com.framsticks.params.CastFailure;
    4 import com.framsticks.util.Casting;
    5 import com.framsticks.util.Containers;
    6 import com.framsticks.util.Numbers;
    7 
    8 import java.util.List;
     3import com.framsticks.params.CompositeParam;
    94
    105/**
     
    1712        }
    1813
    19 
    20 
    21 
    22 
    23 
    2414}
  • java/main/src/main/java/com/framsticks/params/types/ObjectParam.java

    r77 r84  
    33import com.framsticks.params.AccessInterface;
    44import com.framsticks.params.CastFailure;
     5import com.framsticks.params.CompositeParam;
     6import com.framsticks.params.ReassignResult;
    57
    68/**
     
    1315        }
    1416
    15     @Override
    16     public String computeAccessId() {
    17         return containedTypeName;
    18     }
     17        @Override
     18        public String computeAccessId() {
     19                return containedTypeName;
     20        }
    1921
    20     @Override
    21         public Class getStorageType() {
     22        @Override
     23        public Class<?> getStorageType() {
    2224                return Object.class;
    2325        }
    2426
    2527        @Override
    26         public boolean isEmptyAvailable() {
    27                 return true;
     28        public ReassignResult<Object> reassign(Object newValue, Object oldValue) throws CastFailure {
     29                return ReassignResult.create(newValue);
    2830        }
    2931
    3032        @Override
    31         public Object reassign(Object newValue, Object oldValue) throws CastFailure {
    32                 return newValue;
     33        public AccessInterface prepareAccessInterface(AccessInterface access) {
     34                return access;
    3335        }
    3436
    35     @Override
    36     public AccessInterface prepareAccessInterface(AccessInterface access) {
    37         return access;
    38     }
    39 
    40     @Override
    41     public String getType() {
    42         return "o " + containedTypeName;
    43     }
    44 
    45 
    46 
     37        @Override
     38        public String getFramsTypeName() {
     39                return "o " + containedTypeName;
     40        }
    4741
    4842}
  • java/main/src/main/java/com/framsticks/params/types/ProcedureParam.java

    r77 r84  
    11package com.framsticks.params.types;
    22
    3 import com.framsticks.params.CastFailure;
    4 import com.framsticks.params.FramsClass;
    53import com.framsticks.params.Param;
    64import com.framsticks.params.ParamBuilder;
    7 import com.framsticks.util.Strings;
     5import com.framsticks.util.lang.Strings;
    86
    97import java.util.ArrayList;
     
    5856
    5957        @Override
    60         public Class getStorageType() {
     58        public Class<?> getStorageType() {
    6159                return Void.class;
    6260        }
     
    7068        }
    7169
    72     @Override
    73     public Object reassign(Object newValue, Object oldValue) throws CastFailure {
    74         throw new CastFailure();
    75     }
    76 
    77     @Override
    78     public String getType() {
    79         return "p";
    80     }
     70        @Override
     71        public String getFramsTypeName() {
     72                return "p";
     73        }
    8174
    8275}
  • java/main/src/main/java/com/framsticks/params/types/StringParam.java

    r77 r84  
    22
    33import com.framsticks.params.CastFailure;
     4import com.framsticks.params.PrimitiveParam;
     5import com.framsticks.params.ReassignResult;
    46import com.framsticks.params.SinkInterface;
    57
     
    79 * @author Piotr Sniegowski
    810 */
    9 public class StringParam extends ValueParam {
     11public class StringParam extends PrimitiveParam {
    1012        @Override
    11         public Class getStorageType() {
     13        public Class<?> getStorageType() {
    1214                return String.class;
    1315        }
    1416
    1517        @Override
    16         public boolean isEmptyAvailable() {
    17                 return true;
     18        public ReassignResult<String> reassign(Object newValue, Object oldValue) throws CastFailure {
     19                if (newValue == null) {
     20                        return new ReassignResult<String>(null);
     21                }
     22                if (newValue instanceof String) {
     23                        return ReassignResult.create((String) newValue);
     24                }
     25                return ReassignResult.create(newValue.toString());
     26                // return super.reassign(newValue, oldValue);
    1827        }
    1928
    20     @Override
    21     public Object reassign(Object newValue, Object oldValue) throws CastFailure {
    22         if (newValue instanceof String) {
    23             return (String)newValue;
    24         }
    25         return super.reassign(newValue, oldValue);
    26     }
     29        @Override
     30        public String getFramsTypeName() {
     31                return "s";
     32        }
    2733
    28     @Override
    29     public String getType() {
    30         return "s";
    31     }
     34        private static final String SEPARATOR = System.getProperty("line.separator");
    3235
    33     private static final String SEPARATOR = System.getProperty("line.separator");
    34 
    35     @Override
    36     public void save(SinkInterface sink, Object value) {
    37         assert value instanceof String;
    38         String s = (String)value;
    39         sink.print((s.contains(SEPARATOR) ? (s = "~" + SEPARATOR + s + "~") : s));
    40     }
     36        @Override
     37        public void save(SinkInterface sink, Object value) {
     38                assert value instanceof String;
     39                String s = (String)value;
     40                sink.print((s.contains(SEPARATOR) ? (s = "~" + SEPARATOR + s + "~") : s));
     41        }
    4142}
  • java/main/src/main/java/com/framsticks/params/types/UniqueListParam.java

    r77 r84  
    33import com.framsticks.params.AccessInterface;
    44import com.framsticks.params.CastFailure;
     5import com.framsticks.params.ReassignResult;
    56import com.framsticks.params.UniqueListAccess;
    6 import com.framsticks.util.Numbers;
     7import com.framsticks.util.lang.Numbers;
    78
    89import java.util.HashMap;
    9 import java.util.List;
    1010import java.util.Map;
    1111
     
    1515public class UniqueListParam extends ListParam {
    1616
    17     final String uidName;
     17        final String uidName;
    1818
    19     public UniqueListParam(String containedTypeName, String uidName) {
    20         super(containedTypeName);
    21         this.uidName = uidName;
    22     }
     19        public UniqueListParam(String containedTypeName, String uidName) {
     20                super(containedTypeName);
     21                this.uidName = uidName;
     22        }
    2323
    24     @Override
    25     public String computeAccessId() {
    26         return "l " + containedTypeName + " " + uidName;
    27     }
     24        @Override
     25        public String computeAccessId() {
     26                return "l " + containedTypeName + " " + uidName;
     27        }
    2828
    2929
    30     @Override
    31     public Class getStorageType() {
    32         return Map.class;
    33     }
     30        @Override
     31        public Class<?> getStorageType() {
     32                return Map.class;
     33        }
    3434
    35     @Override
    36     public AccessInterface prepareAccessInterface(AccessInterface access) {
    37         return new UniqueListAccess(access, uidName);
    38     }
     35        @Override
     36        public AccessInterface prepareAccessInterface(AccessInterface access) {
     37                return new UniqueListAccess(access, uidName);
     38        }
    3939
     40        @Override
     41        public ReassignResult<? extends Map<?,?>> reassign(Object newValue, Object oldValue) throws CastFailure {
     42                if (newValue instanceof Map) {
     43                        return new ReassignResult<Map<?,?>>((Map<?,?>) newValue);
     44                }
     45                Integer size = Numbers.cast(newValue, Integer.class);
     46                if (size != null) {
     47                        //return oldValue;
     48                        /*
     49                        the integer value should be ignored, because this may cause, that object is created before
     50                        information about it's elements is available, which would break resolution flow
     51                        */
     52                        if (oldValue != null) {
     53                                return new ReassignResult<Map<?,?>>((Map<?,?>) oldValue);
     54                        }
     55                        return ReassignResult.create(new HashMap<Object, Object>());
     56                }
    4057
    41     @Override
    42     public Object reassign(Object newValue, Object oldValue) throws CastFailure {
    43         if (newValue instanceof Map) {
    44             return newValue;
    45         }
    46         Integer size = Numbers.cast(newValue, Integer.class);
    47         if (size != null) {
    48             //return oldValue;
    49             /*
    50             the integer value should be ignored, because this may cause, that object is created before
    51             information about it's elements is available, which would break resolution flow
    52             */
    53             if (oldValue != null) {
    54                 return oldValue;
    55             }
    56             return new HashMap();
    57         }
     58                throw new CastFailure();
     59        }
    5860
    59         throw new CastFailure();
    60     }
    61 
    62     @Override
    63     public String getType() {
    64         return "l " + containedTypeName + " " + uidName;
    65     }
     61        @Override
     62        public String getFramsTypeName() {
     63                return "l " + containedTypeName + " " + uidName;
     64        }
    6665
    6766
  • java/main/src/main/java/com/framsticks/params/types/UniversalParam.java

    r77 r84  
    22
    33import com.framsticks.params.CastFailure;
    4 import com.framsticks.util.Numbers;
     4import com.framsticks.params.PrimitiveParam;
     5import com.framsticks.params.ReassignResult;
    56
    67/**
    78 * @author Piotr Sniegowski
    89 */
    9 public class UniversalParam extends ValueParam {
     10public class UniversalParam extends PrimitiveParam {
    1011        @Override
    11         public Class getStorageType() {
     12        public Class<?> getStorageType() {
    1213                return Object.class;
    1314        }
    1415
    1516        @Override
    16         public boolean isEmptyAvailable() {
    17                 return true;
     17        public String getFramsTypeName() {
     18                return "x";
    1819        }
    1920
    20     @Override
    21     public String getType() {
    22         return "x";
    23     }
    24 
     21        @Override
     22        public ReassignResult<Object> reassign(Object newValue, Object oldValue) throws CastFailure {
     23                return ReassignResult.create(newValue);
     24        }
    2525
    2626}
Note: See TracChangeset for help on using the changeset viewer.