1 /*
   2  * Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 package com.sun.hotspot.igv.data;
  25 
  26 import java.io.Serializable;
  27 import java.util.*;
  28 import java.util.regex.Matcher;
  29 import java.util.regex.Pattern;
  30 import java.util.regex.PatternSyntaxException;
  31 
  32 /**
  33  *
  34  * @author Thomas Wuerthinger
  35  */
  36 public class Properties implements Serializable, Iterable<Property> {
  37 
  38     public static final long serialVersionUID = 1L;
  39     private String[] map = new String[4];
  40 
  41     public Properties() {
  42     }
  43 
  44     @Override
  45     public boolean equals(java.lang.Object o) {
  46         if (!(o instanceof Properties)) {
  47             return false;
  48         }
  49 
  50         Properties p = (Properties) o;
  51 
  52         for (Property prop : this) {
  53             String value = p.get(prop.getName());
  54             if (value == null || !value.equals(prop.getValue())) {
  55                 return false;
  56             }
  57         }
  58 
  59         for (Property prop : p) {
  60             String value = this.get(prop.getName());
  61             if (value == null || !value.equals(prop.getValue())) {
  62                 return false;
  63             }
  64         }
  65 
  66         return true;
  67     }
  68 
  69     @Override
  70     public int hashCode() {
  71         int hash = 5;
  72 
  73         if (map != null) {
  74             for (int i = 0; i < this.map.length; i++) {
  75                 if (map[i] == null) {
  76                     i++;
  77                 } else {
  78                     hash = hash * 83 + map[i].hashCode();
  79                 }
  80             }
  81         }
  82         return hash;
  83     }
  84 
  85     public Properties(String name, String value) {
  86         this();
  87         this.setProperty(name, value);
  88     }
  89 
  90     public Properties(String name, String value, String name1, String value1) {
  91         this(name, value);
  92         this.setProperty(name1, value1);
  93     }
  94 
  95     public Properties(String name, String value, String name1, String value1, String name2, String value2) {
  96         this(name, value, name1, value1);
  97         this.setProperty(name2, value2);
  98     }
  99 
 100     public Properties(Properties p) {
 101         map = new String[p.map.length];
 102         System.arraycopy(p.map, 0, map, 0, p.map.length);
 103     }
 104 
 105     public static class Entity implements Provider {
 106 
 107         private Properties properties;
 108 
 109         public Entity() {
 110             properties = new Properties();
 111         }
 112 
 113         public Entity(Properties.Entity object) {
 114             properties = new Properties(object.getProperties());
 115         }
 116 
 117         @Override
 118         public Properties getProperties() {
 119             return properties;
 120         }
 121     }
 122 
 123     public interface PropertyMatcher {
 124 
 125         String getName();
 126 
 127         boolean match(String value);
 128     }
 129 
 130     public static class InvertPropertyMatcher implements PropertyMatcher {
 131 
 132         private PropertyMatcher matcher;
 133 
 134         public InvertPropertyMatcher(PropertyMatcher matcher) {
 135             this.matcher = matcher;
 136         }
 137 
 138         @Override
 139         public String getName() {
 140             return matcher.getName();
 141         }
 142 
 143         @Override
 144         public boolean match(String p) {
 145             if (p == null) {
 146                 return false;
 147             }
 148             return !matcher.match(p);
 149         }
 150     }
 151 
 152     public static class StringPropertyMatcher implements PropertyMatcher {
 153 
 154         private String name;
 155         private String value;
 156 
 157         public StringPropertyMatcher(String name, String value) {
 158             if (name == null) {
 159                 throw new IllegalArgumentException("Property name must not be null!");
 160             }
 161             if (value == null) {
 162                 throw new IllegalArgumentException("Property value must not be null!");
 163             }
 164             this.name = name;
 165             this.value = value;
 166         }
 167 
 168         @Override
 169         public String getName() {
 170             return name;
 171         }
 172 
 173         @Override
 174         public boolean match(String p) {
 175             if (p == null) {
 176                 throw new IllegalArgumentException("Property value must not be null!");
 177             }
 178             return p.equals(value);
 179         }
 180     }
 181 
 182     public static class RegexpPropertyMatcher implements PropertyMatcher {
 183 
 184         private String name;
 185         private Pattern valuePattern;
 186 
 187         public RegexpPropertyMatcher(String name, String value) {
 188             this(name, value, 0);
 189         }
 190 
 191         public RegexpPropertyMatcher(String name, String value, int flags) {
 192 
 193             if (name == null) {
 194                 throw new IllegalArgumentException("Property name must not be null!");
 195             }
 196 
 197             if (value == null) {
 198                 throw new IllegalArgumentException("Property value pattern must not be null!");
 199             }
 200 
 201             this.name = name;
 202 
 203             try {
 204                 valuePattern = Pattern.compile(value, flags);
 205             } catch (PatternSyntaxException e) {
 206                 throw new IllegalArgumentException("Bad pattern: " + value);
 207             }
 208         }
 209 
 210         @Override
 211         public String getName() {
 212             return name;
 213         }
 214 
 215         @Override
 216         public boolean match(String p) {
 217             if (p == null) {
 218                 throw new IllegalArgumentException("Property value must not be null!");
 219             }
 220             Matcher m = valuePattern.matcher(p);
 221             return m.matches();
 222         }
 223     }
 224 
 225     public Property selectSingle(PropertyMatcher matcher) {
 226 
 227         final String name = matcher.getName();
 228         String value = null;
 229         for (int i = 0; i < map.length; i += 2) {
 230             if (map[i] != null && name.equals(map[i])) {
 231                 value = map[i + 1];
 232                 break;
 233             }
 234         }
 235         if (value != null && matcher.match(value)) {
 236             return new Property(name, value);
 237         } else {
 238             return null;
 239         }
 240     }
 241 
 242     public interface Provider {
 243 
 244         public Properties getProperties();
 245     }
 246 
 247     @Override
 248     public String toString() {
 249         List<String[]> pairs = new ArrayList<>();
 250         for (int i = 0; i < map.length; i += 2) {
 251             if (map[i + 1] != null) {
 252                 pairs.add(new String[]{map[i], map[i + 1]});
 253             }
 254         }
 255 
 256         Collections.sort(pairs, new Comparator<String[]>() {
 257             @Override
 258             public int compare(String[] o1, String[] o2) {
 259                 assert o1.length == 2;
 260                 assert o2.length == 2;
 261                 return o1[0].compareTo(o2[0]);
 262             }
 263         });
 264 
 265         StringBuilder sb = new StringBuilder();
 266         sb.append("[");
 267         boolean first = true;
 268         for (String[] p : pairs) {
 269             if (first) {
 270                 first = false;
 271             } else {
 272                 sb.append(", ");
 273             }
 274             sb.append(p[0]).append("=").append(p[1]);
 275         }
 276         return sb.append("]").toString();
 277     }
 278 
 279     public static class PropertySelector<T extends Properties.Provider> {
 280 
 281         private Collection<T> objects;
 282 
 283         public PropertySelector(Collection<T> objects) {
 284             this.objects = objects;
 285         }
 286 
 287         public T selectSingle(PropertyMatcher matcher) {
 288 
 289             for (T t : objects) {
 290                 Property p = t.getProperties().selectSingle(matcher);
 291                 if (p != null) {
 292                     return t;
 293                 }
 294             }
 295 
 296             return null;
 297         }
 298 
 299         public List<T> selectMultiple(PropertyMatcher matcher) {
 300             List<T> result = new ArrayList<>();
 301 
 302             for (T t : objects) {
 303                 Property p = t.getProperties().selectSingle(matcher);
 304                 if (p != null) {
 305                     result.add(t);
 306                 }
 307             }
 308 
 309             return result;
 310         }
 311     }
 312 
 313     public String get(String key) {
 314         for (int i = 0; i < map.length; i += 2) {
 315             if (map[i] != null && map[i].equals(key)) {
 316                 return map[i + 1];
 317             }
 318         }
 319         return null;
 320     }
 321 
 322     public void setProperty(String name, String value) {
 323         setPropertyInternal(name.intern(), value != null ? value.intern() : null);
 324     }
 325     private void setPropertyInternal(String name, String value) {
 326 
 327         for (int i = 0; i < map.length; i += 2) {
 328             if (map[i] != null && map[i].equals(name)) {
 329                 String p = map[i + 1];
 330                 if (value == null) {
 331                     // remove this property
 332                     map[i] = null;
 333                     map[i + 1] = null;
 334                 } else {
 335                     map[i + 1] = value;
 336                 }
 337                 return;
 338             }
 339         }
 340         if (value == null) {
 341             return;
 342         }
 343         for (int i = 0; i < map.length; i += 2) {
 344             if (map[i] == null) {
 345                 map[i] = name;
 346                 map[i + 1] = value;
 347                 return;
 348             }
 349         }
 350         String[] newMap = new String[map.length + 4];
 351         System.arraycopy(map, 0, newMap, 0, map.length);
 352         newMap[map.length] = name;
 353         newMap[map.length + 1] = value;
 354         map = newMap;
 355     }
 356 
 357     public void add(Properties properties) {
 358         for (Property p : properties) {
 359             // Already interned
 360             setPropertyInternal(p.getName(), p.getValue());
 361         }
 362     }
 363 
 364     private class PropertiesIterator implements Iterator<Property> {
 365 
 366         int index;
 367 
 368         @Override
 369         public boolean hasNext() {
 370             while (index < map.length && map[index + 1] == null) {
 371                 index += 2;
 372             }
 373             return index < map.length;
 374         }
 375 
 376         @Override
 377         public Property next() {
 378             if (index < map.length) {
 379                 index += 2;
 380                 return new Property(map[index - 2], map[index - 1]);
 381             }
 382             return null;
 383         }
 384 
 385         @Override
 386         public void remove() {
 387             throw new UnsupportedOperationException("Not supported yet.");
 388         }
 389     }
 390 
 391     @Override
 392     public Iterator<Property> iterator() {
 393         return new PropertiesIterator();
 394     }
 395 }