Changeset 6823 in josm for trunk/src


Ignore:
Timestamp:
2014-02-07T01:34:02+01:00 (6 years ago)
Author:
bastiK
Message:

#8902 Small performance enhancements / coding style (patch by shinigami, parts)

patterns.diff: precompiled patterns
(only those parts applied, where performance may be relevant)

Location:
trunk/src/org/openstreetmap/josm
Files:
5 edited

Legend:

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

    r6317 r6823  
    1616import java.util.Map.Entry;
    1717import java.util.Set;
     18import java.util.regex.Pattern;
     19
    1820import org.openstreetmap.josm.tools.Utils;
    1921
    2022/**
    2123 * TagCollection is a collection of tags which can be used to manipulate
    22  * tags managed by {@link OsmPrimitive}s.
     24 * tags managed by {@link org.openstreetmap.josm.data.osm.OsmPrimitive}s.
    2325 *
    2426 * A TagCollection can be created:
    2527 * <ul>
    26  *  <li>from the tags managed by a specific {@link OsmPrimitive} with {@link #from(Tagged)}</li>
    27  *  <li>from the union of all tags managed by a collection of {@link OsmPrimitive}s with {@link #unionOfAllPrimitives(Collection)}</li>
    28  *  <li>from the union of all tags managed by a {@link DataSet} with {@link #unionOfAllPrimitives(DataSet)}</li>
    29  *  <li>from the intersection of all tags managed by a collection of primitives with {@link #commonToAllPrimitives(Collection)}</li>
     28 *  <li>from the tags managed by a specific {@link org.openstreetmap.josm.data.osm.OsmPrimitive} with {@link #from(org.openstreetmap.josm.data.osm.Tagged)}</li>
     29 *  <li>from the union of all tags managed by a collection of {@link org.openstreetmap.josm.data.osm.OsmPrimitive}s with {@link #unionOfAllPrimitives(java.util.Collection)}</li>
     30 *  <li>from the union of all tags managed by a {@link org.openstreetmap.josm.data.osm.DataSet} with {@link #unionOfAllPrimitives(org.openstreetmap.josm.data.osm.DataSet)}</li>
     31 *  <li>from the intersection of all tags managed by a collection of primitives with {@link #commonToAllPrimitives(java.util.Collection)}</li>
    3032 * </ul>
    3133 *
     
    3335 *
    3436 * Basic set operations allow to create the union, the intersection and  the difference
    35  * of tag collections, see {@link #union(TagCollection)}, {@link #intersect(TagCollection)},
    36  * and {@link #minus(TagCollection)}.
     37 * of tag collections, see {@link #union(org.openstreetmap.josm.data.osm.TagCollection)}, {@link #intersect(org.openstreetmap.josm.data.osm.TagCollection)},
     38 * and {@link #minus(org.openstreetmap.josm.data.osm.TagCollection)}.
    3739 *
    3840 *
     
    4244    /**
    4345     * Creates a tag collection from the tags managed by a specific
    44      * {@link OsmPrimitive}. If <code>primitive</code> is null, replies
     46     * {@link org.openstreetmap.josm.data.osm.OsmPrimitive}. If <code>primitive</code> is null, replies
    4547     * an empty tag collection.
    4648     *
    4749     * @param primitive  the primitive
    4850     * @return a tag collection with the tags managed by a specific
    49      * {@link OsmPrimitive}
     51     * {@link org.openstreetmap.josm.data.osm.OsmPrimitive}
    5052     */
    5153    public static TagCollection from(Tagged primitive) {
     
    590592
    591593    /**
    592      * Applies this tag collection to an {@link OsmPrimitive}. Does nothing if
     594     * Applies this tag collection to an {@link org.openstreetmap.josm.data.osm.OsmPrimitive}. Does nothing if
    593595     * primitive is null
    594596     *
     
    611613
    612614    /**
    613      * Applies this tag collection to a collection of {@link OsmPrimitive}s. Does nothing if
     615     * Applies this tag collection to a collection of {@link org.openstreetmap.josm.data.osm.OsmPrimitive}s. Does nothing if
    614616     * primitives is null
    615617     *
     
    628630
    629631    /**
    630      * Replaces the tags of an {@link OsmPrimitive} by the tags in this collection . Does nothing if
     632     * Replaces the tags of an {@link org.openstreetmap.josm.data.osm.OsmPrimitive} by the tags in this collection . Does nothing if
    631633     * primitive is null
    632634     *
     
    646648
    647649    /**
    648      * Replaces the tags of a collection of{@link OsmPrimitive}s by the tags in this collection.
     650     * Replaces the tags of a collection of{@link org.openstreetmap.josm.data.osm.OsmPrimitive}s by the tags in this collection.
    649651     * Does nothing if primitives is null
    650652     *
     
    716718    }
    717719
     720    private static final Pattern SPLIT_VALUES_PATTERN = Pattern.compile(";\\s*");
     721
    718722    /**
    719723     * Replies the concatenation of all tag values (concatenated by a semicolon)
     
    732736        Map<String, Collection<String>> originalSplitValues = new LinkedHashMap<String, Collection<String>>();
    733737        for (String v : originalValues) {
    734             List<String> vs = Arrays.asList(v.split(";\\s*"));
     738            List<String> vs = Arrays.asList(SPLIT_VALUES_PATTERN.split(v));
    735739            originalSplitValues.put(v, vs);
    736740            values.addAll(vs);
  • trunk/src/org/openstreetmap/josm/data/projection/CustomProjection.java

    r6798 r6823  
    157157    private Map<String, String> parseParameterList(String pref) throws ProjectionConfigurationException {
    158158        Map<String, String> parameters = new HashMap<String, String>();
    159         String[] parts = pref.trim().split("\\s+");
     159        String[] parts = Utils.WHITE_SPACES_PATTERN.split(pref.trim());
    160160        if (pref.trim().isEmpty()) {
    161161            parts = new String[0];
  • trunk/src/org/openstreetmap/josm/data/validation/tests/NameMismatch.java

    r6591 r6823  
    77import java.util.HashSet;
    88import java.util.Map.Entry;
     9import java.util.regex.Pattern;
    910
    1011import org.openstreetmap.josm.data.osm.OsmPrimitive;
     
    3233    protected static final int NAME_MISSING = 1501;
    3334    protected static final int NAME_TRANSLATION_MISSING = 1502;
     35    private static final Pattern NAME_SPLIT_PATTERN = Pattern.compile(" - ");
    3436
    3537    /**
     
    8688        Check if this is the case. */
    8789
    88         String[] splitNames = name.split(" - ");
     90        String[] splitNames = NAME_SPLIT_PATTERN.split(name);
    8991        if (splitNames.length == 1) {
    9092            /* The name is not composed of multiple parts. Complain. */
  • trunk/src/org/openstreetmap/josm/data/validation/tests/TagChecker.java

    r6783 r6823  
    669669        }
    670670
    671         public String getData(String str) {
    672             Matcher m = Pattern.compile(" *# *([^#]+) *$").matcher(str);
    673             str = m.replaceFirst("").trim();
     671        private static final Pattern CLEAN_STR_PATTERN = Pattern.compile(" *# *([^#]+) *$");
     672        private static final Pattern SPLIT_TRIMMED_PATTERN = Pattern.compile(" *: *");
     673        private static final Pattern SPLIT_ELEMENTS_PATTERN = Pattern.compile(" *&& *");
     674
     675        public String getData(final String str) {
     676            Matcher m = CLEAN_STR_PATTERN.matcher(str);
     677            String trimmed = m.replaceFirst("").trim();
    674678            try {
    675679                description = m.group(1);
     
    680684                description = null;
    681685            }
    682             String[] n = str.split(" *: *", 3);
     686            String[] n = SPLIT_TRIMMED_PATTERN.split(trimmed, 3);
    683687            if (n[0].equals("way")) {
    684688                type = OsmPrimitiveType.WAY;
     
    705709            } else
    706710                return tr("Could not find warning level");
    707             for (String exp: n[2].split(" *&& *")) {
     711            for (String exp: SPLIT_ELEMENTS_PATTERN.split(n[2])) {
    708712                try {
    709713                    data.add(new CheckerElement(exp));
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r6806 r6823  
    4141import java.util.List;
    4242import java.util.regex.Matcher;
     43import java.util.regex.Pattern;
    4344import java.util.zip.GZIPInputStream;
    4445import java.util.zip.ZipFile;
     
    5354 */
    5455public final class Utils {
     56
     57    public static final Pattern WHITE_SPACES_PATTERN = Pattern.compile("\\s+");
    5558
    5659    private Utils() {
     
    318321     * @param in The source file
    319322     * @param out The destination file
    320      * @throws IOException If any I/O error occurs
     323     * @throws java.io.IOException If any I/O error occurs
    321324     */
    322325    public static void copyFile(File in, File out) throws IOException  {
     
    365368
    366369    /**
    367      * <p>Utility method for closing a {@link Closeable} object.</p>
     370     * <p>Utility method for closing a {@link java.io.Closeable} object.</p>
    368371     *
    369372     * @param c the closeable object. May be null.
     
    379382
    380383    /**
    381      * <p>Utility method for closing a {@link ZipFile}.</p>
     384     * <p>Utility method for closing a {@link java.util.zip.ZipFile}.</p>
    382385     *
    383386     * @param zip the zip file. May be null.
     
    577580    /**
    578581     * Transforms the collection {@code c} into an unmodifiable collection and
    579      * applies the {@link Function} {@code f} on each element upon access.
     582     * applies the {@link org.openstreetmap.josm.tools.Utils.Function} {@code f} on each element upon access.
    580583     * @param <A> class of input collection
    581584     * @param <B> class of transformed collection
     
    619622    /**
    620623     * Transforms the list {@code l} into an unmodifiable list and
    621      * applies the {@link Function} {@code f} on each element upon access.
     624     * applies the {@link org.openstreetmap.josm.tools.Utils.Function} {@code f} on each element upon access.
    622625     * @param <A> class of input collection
    623626     * @param <B> class of transformed collection
     
    644647    }
    645648
     649    private static final Pattern HTTP_PREFFIX_PATTERN = Pattern.compile("https?");
     650
    646651    /**
    647652     * Opens a HTTP connection to the given URL and sets the User-Agent property to JOSM's one.
    648653     * @param httpURL The HTTP url to open (must use http:// or https://)
    649654     * @return An open HTTP connection to the given URL
    650      * @throws IOException if an I/O exception occurs.
     655     * @throws java.io.IOException if an I/O exception occurs.
    651656     * @since 5587
    652657     */
    653658    public static HttpURLConnection openHttpConnection(URL httpURL) throws IOException {
    654         if (httpURL == null || !httpURL.getProtocol().matches("https?")) {
     659        if (httpURL == null || !HTTP_PREFFIX_PATTERN.matcher(httpURL.getProtocol()).matches()) {
    655660            throw new IllegalArgumentException("Invalid HTTP url");
    656661        }
     
    665670     * @param url The url to open
    666671     * @return An stream for the given URL
    667      * @throws IOException if an I/O exception occurs.
     672     * @throws java.io.IOException if an I/O exception occurs.
    668673     * @since 5867
    669674     */
     
    711716     * @param url The url to open
    712717     * @return An buffered stream reader for the given URL (using UTF-8)
    713      * @throws IOException if an I/O exception occurs.
     718     * @throws java.io.IOException if an I/O exception occurs.
    714719     * @since 5868
    715720     */
     
    736741     * @param keepAlive whether not to set header {@code Connection=close}
    737742     * @return An open HTTP connection to the given URL
    738      * @throws IOException if an I/O exception occurs.
     743     * @throws java.io.IOException if an I/O exception occurs.
    739744     * @since 5587
    740745     */
Note: See TracChangeset for help on using the changeset viewer.