< prev index next >

src/share/tools/IdealGraphVisualizer/Data/src/com/sun/hotspot/igv/data/Properties.java

Print this page


   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) {


  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();


 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;


   1 /*
   2  * Copyright (c) 2008, 2016, 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.lang.ref.WeakReference;
  28 import java.util.*;
  29 import java.util.Map.Entry;
  30 import java.util.regex.Matcher;
  31 import java.util.regex.Pattern;
  32 import java.util.regex.PatternSyntaxException;
  33 
  34 /**
  35  *
  36  * @author Thomas Wuerthinger
  37  */
  38 public class Properties implements Serializable, Iterable<Property> {
  39 
  40     public static final long serialVersionUID = 1L;
  41     protected String[] map = new String[4];
  42 
  43     public Properties() {
  44     }
  45 
  46     @Override
  47     public boolean equals(java.lang.Object o) {
  48         if (!(o instanceof Properties)) {
  49             return false;
  50         }
  51 
  52         Properties p = (Properties) o;
  53 
  54         for (Property prop : this) {
  55             String value = p.get(prop.getName());
  56             if (value == null || !value.equals(prop.getValue())) {
  57                 return false;
  58             }
  59         }
  60 
  61         for (Property prop : p) {


  87     public Properties(String name, String value) {
  88         this();
  89         this.setProperty(name, value);
  90     }
  91 
  92     public Properties(String name, String value, String name1, String value1) {
  93         this(name, value);
  94         this.setProperty(name1, value1);
  95     }
  96 
  97     public Properties(String name, String value, String name1, String value1, String name2, String value2) {
  98         this(name, value, name1, value1);
  99         this.setProperty(name2, value2);
 100     }
 101 
 102     public Properties(Properties p) {
 103         map = new String[p.map.length];
 104         System.arraycopy(p.map, 0, map, 0, p.map.length);
 105     }
 106 
 107     protected Properties(String[] map) {
 108         this.map = map;
 109     }
 110 
 111     static class SharedProperties extends Properties {
 112         int hashCode;
 113 
 114         SharedProperties(String[] map) {
 115             super(map);
 116             this.hashCode = Arrays.hashCode(map);
 117         }
 118 
 119         @Override
 120         protected void setPropertyInternal(String name, String value) {
 121             throw new UnsupportedOperationException();
 122         }
 123 
 124         @Override
 125         public boolean equals(Object other) {
 126             if (this == other) {
 127                 return true;
 128             }
 129             if (!(other instanceof SharedProperties)) {
 130                 return super.equals(other);
 131             }
 132             SharedProperties props2 = (SharedProperties) other;
 133             return Arrays.equals(map, props2.map);
 134         }
 135 
 136         @Override
 137         public int hashCode() {
 138             return hashCode;
 139         }
 140     }
 141 
 142     private static class PropertyCache {
 143         static WeakHashMap<SharedProperties, WeakReference<SharedProperties>> immutableCache = new WeakHashMap<>();
 144 
 145         static synchronized SharedProperties intern(Properties properties) {
 146             String[] map = properties.map;
 147             SharedProperties key = new SharedProperties(map);
 148             WeakReference<SharedProperties> entry = immutableCache.get(key);
 149             if (entry != null) {
 150                 SharedProperties props = entry.get();
 151                 if (props != null) {
 152                     return props;
 153                 }
 154             }
 155             immutableCache.put(key, new WeakReference<>(key));
 156             return key;
 157         }
 158     }
 159 
 160     public static class Entity implements Provider {
 161 
 162         private Properties properties;
 163 
 164         public Entity() {
 165             properties = new Properties();
 166         }
 167 
 168         public Entity(Properties.Entity object) {
 169             properties = new Properties(object.getProperties());
 170         }
 171 
 172         @Override
 173         public Properties getProperties() {
 174             return properties;
 175         }
 176 
 177         public void internProperties() {
 178             properties = PropertyCache.intern(properties);
 179         }
 180     }
 181 
 182     public interface PropertyMatcher {
 183 
 184         String getName();
 185 
 186         boolean match(String value);
 187     }
 188 
 189     public static class InvertPropertyMatcher implements PropertyMatcher {
 190 
 191         private PropertyMatcher matcher;
 192 
 193         public InvertPropertyMatcher(PropertyMatcher matcher) {
 194             this.matcher = matcher;
 195         }
 196 
 197         @Override
 198         public String getName() {
 199             return matcher.getName();


 364                     result.add(t);
 365                 }
 366             }
 367 
 368             return result;
 369         }
 370     }
 371 
 372     public String get(String key) {
 373         for (int i = 0; i < map.length; i += 2) {
 374             if (map[i] != null && map[i].equals(key)) {
 375                 return map[i + 1];
 376             }
 377         }
 378         return null;
 379     }
 380 
 381     public void setProperty(String name, String value) {
 382         setPropertyInternal(name.intern(), value != null ? value.intern() : null);
 383     }

 384 
 385     protected void setPropertyInternal(String name, String value) {
 386         for (int i = 0; i < map.length; i += 2) {
 387             if (map[i] != null && map[i].equals(name)) {
 388                 String p = map[i + 1];
 389                 if (value == null) {
 390                     // remove this property
 391                     map[i] = null;
 392                     map[i + 1] = null;
 393                 } else {
 394                     map[i + 1] = value;
 395                 }
 396                 return;
 397             }
 398         }
 399         if (value == null) {
 400             return;
 401         }
 402         for (int i = 0; i < map.length; i += 2) {
 403             if (map[i] == null) {
 404                 map[i] = name;
 405                 map[i + 1] = value;


< prev index next >