Changeset 6175 in josm for trunk


Ignore:
Timestamp:
2013-08-22T14:41:30+02:00 (11 years ago)
Author:
Don-vip
Message:

see #8902 - Small performance enhancements / coding style (patches by shinigami):

  • bytestohexstring.diff: byte[] to hex string change + small test
  • collfix.diff : problem in generic collection definition
  • styleiteration.diff: useless iteration to do end of list
  • environment.diff: Environment - merge chained ops (each created copy of env.) to one
Location:
trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/conflict/ConflictCollection.java

    r6104 r6175  
    3737    private CopyOnWriteArrayList<IConflictListener> listeners;
    3838
     39    /**
     40     * Constructs a new {@code ConflictCollection}.
     41     */
    3942    public ConflictCollection() {
    40         conflicts = new ArrayList<Conflict<?>>();
     43        conflicts = new ArrayList<Conflict<? extends OsmPrimitive>>();
    4144        listeners = new CopyOnWriteArrayList<IConflictListener>();
    4245    }
    4346
     47    /**
     48     * Adds the specified conflict listener, if not already present.
     49     * @param listener The conflict listener to add
     50     */
    4451    public void addConflictListener(IConflictListener listener) {
    4552        if (listener != null) {
     
    4855    }
    4956
     57    /**
     58     * Removes the specified conflict listener.
     59     * @param listener The conflict listener to remove
     60     */
    5061    public void removeConflictListener(IConflictListener listener) {
    5162        listeners.remove(listener);
  • trunk/src/org/openstreetmap/josm/gui/mappaint/ElemStyles.java

    r5899 r6175  
    236236                        if (s.isProperLineStyle() || s instanceof AreaElemStyle) {
    237237                            hasIndependentElemStyle = true;
     238                            break;
    238239                        }
    239240                    }
  • trunk/src/org/openstreetmap/josm/gui/mappaint/Environment.java

    r6105 r6175  
    2323
    2424    /**
    25      * If not null, this is the matching parent object if an condition or an expression
     25     * If not null, this is the matching parent object if a condition or an expression
    2626     * is evaluated in a {@link LinkSelector} (within a child selector)
    2727     */
    2828    public OsmPrimitive parent;
     29   
    2930    /**
    3031     * The same for parent selector. Only one of the 2 fields (parent or child) is not null in any environment.
     
    3839
    3940    /**
    40      * Creates a new uninitialized environment
     41     * Creates a new uninitialized environment.
    4142     */
    4243    public Environment() {}
    4344
     45    /**
     46     * Creates a new environment.
     47     */
    4448    public Environment(OsmPrimitive osm, MultiCascade mc, String layer, StyleSource source) {
    4549        this.osm = osm;
     
    5054
    5155    /**
    52      * Creates a clone of the environment {@code other}
     56     * Creates a clone of the environment {@code other}.
    5357     *
    5458     * @param other the other environment. Must not be null.
    55      */
    56     public Environment(Environment other) throws IllegalArgumentException{
     59     * @throws IllegalArgumentException if {@code param} is {@code null}
     60     */
     61    public Environment(Environment other) throws IllegalArgumentException {
    5762        CheckParameterUtil.ensureParameterNotNull(other);
    5863        this.osm = other.osm;
     
    6671    }
    6772
     73    /**
     74     * Creates a clone of this environment, with the specified primitive.
     75     * @return A clone of this environment, with the specified primitive
     76     * @see #osm
     77     */
    6878    public Environment withPrimitive(OsmPrimitive osm) {
    6979        Environment e = new Environment(this);
     
    7282    }
    7383
     84    /**
     85     * Creates a clone of this environment, with the specified parent.
     86     * @param parent the matching parent object
     87     * @return A clone of this environment, with the specified parent
     88     * @see #parent
     89     */
    7490    public Environment withParent(OsmPrimitive parent) {
    7591        Environment e = new Environment(this);
     
    7894    }
    7995
     96    /**
     97     * Creates a clone of this environment, with the specified parent, index, and context set to {@link Context#LINK}.
     98     * @param parent the matching parent object
     99     * @param index index of node in parent way or member in parent relation
     100     * @return A clone of this environment, with the specified parent, index, and context set to {@link Context#LINK}
     101     * @since 6175
     102     * @see #parent
     103     * @see #index
     104     */
     105    public Environment withParentAndIndexAndLinkContext(OsmPrimitive parent, int index) {
     106        Environment e = new Environment(this);
     107        e.parent = parent;
     108        e.index = index;
     109        e.context = Context.LINK;
     110        return e;
     111    }
     112
     113    /**
     114     * Creates a clone of this environment, with the specified child.
     115     * @param child the matching child object
     116     * @return A clone of this environment, with the specified child
     117     * @see #child
     118     */
    80119    public Environment withChild(OsmPrimitive child) {
    81120        Environment e = new Environment(this);
     
    84123    }
    85124
     125    /**
     126     * Creates a clone of this environment, with the specified child, index, and context set to {@link Context#LINK}.
     127     * @param child the matching child object
     128     * @param index index of node in parent way or member in parent relation
     129     * @return A clone of this environment, with the specified child, index, and context set to {@code Context#LINK}
     130     * @since 6175
     131     * @see #child
     132     * @see #index
     133     */
     134    public Environment withChildAndIndexAndLinkContext(OsmPrimitive child, int index) {
     135        Environment e = new Environment(this);
     136        e.child = child;
     137        e.index = index;
     138        e.context = Context.LINK;
     139        return e;
     140    }
     141
     142    /**
     143     * Creates a clone of this environment, with the specified index.
     144     * @param index index of node in parent way or member in parent relation
     145     * @return A clone of this environment, with the specified index
     146     * @see #index
     147     */
    86148    public Environment withIndex(int index) {
    87149        Environment e = new Environment(this);
     
    90152    }
    91153
     154    /**
     155     * Creates a clone of this environment, with the specified {@link Context}.
     156     * @return A clone of this environment, with the specified {@code Context}
     157     */
    92158    public Environment withContext(Context context) {
    93159        Environment e = new Environment(this);
     
    96162    }
    97163
     164    /**
     165     * Creates a clone of this environment, with context set to {@link Context#LINK}.
     166     * @return A clone of this environment, with context set to {@code Context#LINK}
     167     */
    98168    public Environment withLinkContext() {
    99169        Environment e = new Environment(this);
     
    102172    }
    103173
     174    /**
     175     * Determines if the context of this environment is {@link Context#LINK}.
     176     * @return {@code true} if the context of this environment is {@code Context#LINK}, {@code false} otherwise
     177     */
    104178    public boolean isLinkContext() {
    105179        return Context.LINK.equals(context);
    106180    }
    107181
     182    /**
     183     * Determines if this environment has a relation as parent.
     184     * @return {@code true} if this environment has a relation as parent, {@code false} otherwise
     185     * @see #parent
     186     */
    108187    public boolean hasParentRelation() {
    109188        return parent instanceof Relation;
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSStyleSource.java

    r6148 r6175  
    173173                        sub = "default";
    174174                    }
    175 
    176                     if (sub.equals("*")) {
     175                    else if ("*".equals(sub)) {
    177176                        for (Entry<String, Cascade> entry : mc.getLayers()) {
    178177                            env.layer = entry.getKey();
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/Selector.java

    r6070 r6175  
    108108                    Node n = w.getNode(i);
    109109                    if (n.equals(e.osm)) {
    110                         if (link.matches(e.withParent(w).withIndex(i).withLinkContext())) {
     110                        if (link.matches(e.withParentAndIndexAndLinkContext(w, i))) {
    111111                            e.parent = w;
    112112                            e.index = i;
     
    130130                    RelationMember m = r.getMember(i);
    131131                    if (m.getMember().equals(e.osm)) {
    132                         if (link.matches(e.withParent(r).withIndex(i).withLinkContext())) {
     132                        if (link.matches(e.withParentAndIndexAndLinkContext(r, i))) {
    133133                            e.parent = r;
    134134                            e.index = i;
     
    156156                        Node n = wayNodes.get(i);
    157157                        if (left.matches(e.withPrimitive(n))) {
    158                             if (link.matches(e.withChild(n).withIndex(i).withLinkContext())) {
     158                            if (link.matches(e.withChildAndIndexAndLinkContext(n, i))) {
    159159                                e.child = n;
    160160                                e.index = i;
     
    169169                        OsmPrimitive member = members.get(i).getMember();
    170170                        if (left.matches(e.withPrimitive(member))) {
    171                             if (link.matches(e.withChild(member).withIndex(i).withLinkContext())) {
     171                            if (link.matches(e.withChildAndIndexAndLinkContext(member, i))) {
    172172                                e.child = member;
    173173                                e.index = i;
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r6123 r6175  
    401401    }
    402402
     403    private static final char[] HEX_ARRAY = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
     404
    403405    /**
    404406     * Converts a byte array to a string of hexadecimal characters.
     
    409411     */
    410412    public static String toHexString(byte[] bytes) {
    411         char[] hexArray = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
    412         char[] hexChars = new char[bytes.length * 2];
    413         for (int j=0; j<bytes.length; j++) {
    414             int v = bytes[j] & 0xFF;
    415             hexChars[j*2] = hexArray[v/16];
    416             hexChars[j*2 + 1] = hexArray[v%16];
     413
     414        if (bytes == null) {
     415            return "";
     416        }
     417
     418        final int len = bytes.length;
     419        if (len == 0) {
     420            return "";
     421        }
     422
     423        char[] hexChars = new char[len * 2];
     424        for (int i = 0, j = 0; i < len; i++) {
     425            final int v = bytes[i];
     426            hexChars[j++] = HEX_ARRAY[(v & 0xf0) >> 4];
     427            hexChars[j++] = HEX_ARRAY[v & 0xf];
    417428        }
    418429        return new String(hexChars);
  • trunk/test/unit/org/openstreetmap/josm/tools/UtilsTest.java

    r5772 r6175  
    5151        Assert.assertEquals("abc", Utils.strip(someWhite+"abc"+someWhite));
    5252    }
     53
     54    /**
     55     * Test of {@link Utils#toHexString} method.
     56     */
     57    @Test
     58    public void testToHexString(){
     59        Assert.assertEquals("", Utils.toHexString(null));
     60        Assert.assertEquals("", Utils.toHexString(new byte[0]));
     61        Assert.assertEquals("01", Utils.toHexString(new byte[]{0x1}));
     62        Assert.assertEquals("0102", Utils.toHexString(new byte[]{0x1,0x2}));
     63        Assert.assertEquals("12", Utils.toHexString(new byte[]{0x12}));
     64        Assert.assertEquals("127f", Utils.toHexString(new byte[]{0x12, 0x7f}));
     65        Assert.assertEquals("fedc", Utils.toHexString(new byte[]{(byte) 0xfe, (byte) 0xdc}));
     66    }
    5367}
Note: See TracChangeset for help on using the changeset viewer.