Ignore:
Timestamp:
2015-09-09T13:03:32+02:00 (9 years ago)
Author:
simon04
Message:

fix #11834 - Use visitor pattern for key/value lookup (patch by michael2402, slightly modified)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/osm/AbstractPrimitive.java

    r8735 r8740  
    2727public abstract class AbstractPrimitive implements IPrimitive {
    2828
     29    /**
     30     * This is a visitor that can be used to loop over the keys/values of this primitive.
     31     *
     32     * @author Michael Zangl
     33     * @since 8740
     34     */
     35    public interface KeyValueVisitor {
     36
     37        /**
     38         * This method gets called for every tag received.
     39         *
     40         * @param key   The key
     41         * @param value The value
     42         */
     43        void visitKeyValue(String key, String value);
     44    }
     45
    2946    private static final AtomicLong idCounter = new AtomicLong(0);
    3047
     
    477494     * @return tags of this primitive. Changes made in returned map are not mapped
    478495     * back to the primitive, use setKeys() to modify the keys
     496     * @see #visitKeys(KeyValueVisitor)
    479497     */
    480498    @Override
     
    489507        }
    490508        return result;
     509    }
     510
     511    /**
     512     * Calls the visitor for every key/value pair of this primitive.
     513     *
     514     * @param visitor The visitor to call.
     515     * @see #getKeys()
     516     * @since 8740
     517     */
     518    public void visitKeys(KeyValueVisitor visitor) {
     519        final String[] keys = this.keys;
     520        if (keys != null) {
     521            for (int i = 0; i < keys.length; i += 2) {
     522                visitor.visitKeyValue(keys[i], keys[i + 1]);
     523            }
     524        }
    491525    }
    492526
Note: See TracChangeset for help on using the changeset viewer.