jdk/src/java.management/share/classes/sun/management/LazyCompositeData.java
changeset 33260 af1ca24593aa
parent 30355 e37c7eba132f
equal deleted inserted replaced
33259:bbb84c5a1228 33260:af1ca24593aa
    25 
    25 
    26 package sun.management;
    26 package sun.management;
    27 
    27 
    28 import java.io.Serializable;
    28 import java.io.Serializable;
    29 import java.util.*;
    29 import java.util.*;
       
    30 import javax.management.openmbean.ArrayType;
    30 import javax.management.openmbean.CompositeData;
    31 import javax.management.openmbean.CompositeData;
    31 import javax.management.openmbean.CompositeType;
    32 import javax.management.openmbean.CompositeType;
    32 import javax.management.openmbean.OpenType;
    33 import javax.management.openmbean.OpenType;
    33 import javax.management.openmbean.TabularType;
    34 import javax.management.openmbean.TabularType;
    34 
    35 
    46         implements CompositeData, Serializable {
    47         implements CompositeData, Serializable {
    47 
    48 
    48     private CompositeData compositeData;
    49     private CompositeData compositeData;
    49 
    50 
    50     // Implementation of the CompositeData interface
    51     // Implementation of the CompositeData interface
       
    52     @Override
    51     public boolean containsKey(String key) {
    53     public boolean containsKey(String key) {
    52         return compositeData().containsKey(key);
    54         return compositeData().containsKey(key);
    53     }
    55     }
    54 
    56 
       
    57     @Override
    55     public boolean containsValue(Object value) {
    58     public boolean containsValue(Object value) {
    56         return compositeData().containsValue(value);
    59         return compositeData().containsValue(value);
    57     }
    60     }
    58 
    61 
       
    62     @Override
    59     public boolean equals(Object obj) {
    63     public boolean equals(Object obj) {
    60         return compositeData().equals(obj);
    64         return compositeData().equals(obj);
    61     }
    65     }
    62 
    66 
       
    67     @Override
    63     public Object get(String key) {
    68     public Object get(String key) {
    64         return compositeData().get(key);
    69         return compositeData().get(key);
    65     }
    70     }
    66 
    71 
       
    72     @Override
    67     public Object[] getAll(String[] keys) {
    73     public Object[] getAll(String[] keys) {
    68         return compositeData().getAll(keys);
    74         return compositeData().getAll(keys);
    69     }
    75     }
    70 
    76 
       
    77     @Override
    71     public CompositeType getCompositeType() {
    78     public CompositeType getCompositeType() {
    72         return compositeData().getCompositeType();
    79         return compositeData().getCompositeType();
    73     }
    80     }
    74 
    81 
       
    82     @Override
    75     public int hashCode() {
    83     public int hashCode() {
    76         return compositeData().hashCode();
    84         return compositeData().hashCode();
    77     }
    85     }
    78 
    86 
       
    87     @Override
    79     public String toString() {
    88     public String toString() {
    80         /** FIXME: What should this be?? */
    89         /** FIXME: What should this be?? */
    81         return compositeData().toString();
    90         return compositeData().toString();
    82     }
    91     }
    83 
    92 
       
    93     @Override
    84     public Collection<?> values() {
    94     public Collection<?> values() {
    85         return compositeData().values();
    95         return compositeData().values();
    86     }
    96     }
    87 
    97 
    88     /* Lazy creation of a CompositeData object
    98     /* Lazy creation of a CompositeData object
   124 
   134 
   125     public static boolean getBoolean(CompositeData cd, String itemName) {
   135     public static boolean getBoolean(CompositeData cd, String itemName) {
   126         if (cd == null)
   136         if (cd == null)
   127             throw new IllegalArgumentException("Null CompositeData");
   137             throw new IllegalArgumentException("Null CompositeData");
   128 
   138 
   129         return ((Boolean) cd.get(itemName)).booleanValue();
   139         return ((Boolean) cd.get(itemName));
   130     }
   140     }
   131 
   141 
   132     public static long getLong(CompositeData cd, String itemName) {
   142     public static long getLong(CompositeData cd, String itemName) {
   133         if (cd == null)
   143         if (cd == null)
   134             throw new IllegalArgumentException("Null CompositeData");
   144             throw new IllegalArgumentException("Null CompositeData");
   135 
   145 
   136         return ((Long) cd.get(itemName)).longValue();
   146         return ((Long) cd.get(itemName));
   137     }
   147     }
   138 
   148 
   139     public static int getInt(CompositeData cd, String itemName) {
   149     public static int getInt(CompositeData cd, String itemName) {
   140         if (cd == null)
   150         if (cd == null)
   141             throw new IllegalArgumentException("Null CompositeData");
   151             throw new IllegalArgumentException("Null CompositeData");
   142 
   152 
   143         return ((Integer) cd.get(itemName)).intValue();
   153         return ((Integer) cd.get(itemName));
   144     }
   154     }
   145 
   155 
   146     /**
   156     /**
   147      * Compares two CompositeTypes and returns true if
   157      * Compares two CompositeTypes and returns true if
   148      * all items in type1 exist in type2 and their item types
   158      * all items in type1 exist in type2 and their item types
   149      * are the same.
   159      * are the same.
       
   160      * @param type1 the base composite type
       
   161      * @param type2 the checked composite type
       
   162      * @return {@code true} if all items in type1 exist in type2 and their item
       
   163      *         types are the same.
   150      */
   164      */
   151     protected static boolean isTypeMatched(CompositeType type1, CompositeType type2) {
   165     protected static boolean isTypeMatched(CompositeType type1, CompositeType type2) {
   152         if (type1 == type2) return true;
   166         if (type1 == type2) return true;
   153 
   167 
   154         // We can't use CompositeType.isValue() since it returns false
   168         // We can't use CompositeType.isValue() since it returns false
   157 
   171 
   158         // Check all items in the type1 exist in type2
   172         // Check all items in the type1 exist in type2
   159         if (!type2.keySet().containsAll(allItems))
   173         if (!type2.keySet().containsAll(allItems))
   160             return false;
   174             return false;
   161 
   175 
   162         for (String item: allItems) {
   176         return allItems.stream().allMatch(
   163             OpenType<?> ot1 = type1.getType(item);
   177             item -> isTypeMatched(type1.getType(item), type2.getType(item))
   164             OpenType<?> ot2 = type2.getType(item);
   178         );
   165             if (ot1 instanceof CompositeType) {
   179     }
   166                 if (! (ot2 instanceof CompositeType))
   180 
   167                     return false;
   181     protected static boolean isTypeMatched(TabularType type1, TabularType type2) {
   168                 if (!isTypeMatched((CompositeType) ot1, (CompositeType) ot2))
   182         if (type1 == type2) return true;
   169                     return false;
   183 
   170             } else if (ot1 instanceof TabularType) {
   184         List<String> list1 = type1.getIndexNames();
   171                 if (! (ot2 instanceof TabularType))
   185         List<String> list2 = type2.getIndexNames();
   172                     return false;
   186 
   173                 if (!isTypeMatched((TabularType) ot1, (TabularType) ot2))
   187         // check if the list of index names are the same
   174                     return false;
   188         if (!list1.equals(list2))
   175             } else if (!ot1.equals(ot2)) {
   189             return false;
       
   190 
       
   191         return isTypeMatched(type1.getRowType(), type2.getRowType());
       
   192     }
       
   193 
       
   194     protected static boolean isTypeMatched(ArrayType<?> type1, ArrayType<?> type2) {
       
   195         if (type1 == type2) return true;
       
   196 
       
   197         int dim1 = type1.getDimension();
       
   198         int dim2 = type2.getDimension();
       
   199 
       
   200         // check if the array dimensions are the same
       
   201         if (dim1 != dim2)
       
   202             return false;
       
   203 
       
   204         return isTypeMatched(type1.getElementOpenType(), type2.getElementOpenType());
       
   205     }
       
   206 
       
   207     private static boolean isTypeMatched(OpenType<?> ot1, OpenType<?> ot2) {
       
   208         if (ot1 instanceof CompositeType) {
       
   209             if (! (ot2 instanceof CompositeType))
       
   210                 return false;
       
   211             if (!isTypeMatched((CompositeType) ot1, (CompositeType) ot2))
       
   212                 return false;
       
   213         } else if (ot1 instanceof TabularType) {
       
   214             if (! (ot2 instanceof TabularType))
       
   215                 return false;
       
   216             if (!isTypeMatched((TabularType) ot1, (TabularType) ot2))
       
   217                 return false;
       
   218         } else if (ot1 instanceof ArrayType) {
       
   219             if (! (ot2 instanceof ArrayType))
       
   220                 return false;
       
   221             if (!isTypeMatched((ArrayType<?>) ot1, (ArrayType<?>) ot2)) {
   176                 return false;
   222                 return false;
   177             }
   223             }
       
   224         } else if (!ot1.equals(ot2)) {
       
   225             return false;
   178         }
   226         }
   179         return true;
   227         return true;
   180     }
   228     }
   181 
   229 
   182     protected static boolean isTypeMatched(TabularType type1, TabularType type2) {
       
   183         if (type1 == type2) return true;
       
   184 
       
   185         List<String> list1 = type1.getIndexNames();
       
   186         List<String> list2 = type2.getIndexNames();
       
   187 
       
   188         // check if the list of index names are the same
       
   189         if (!list1.equals(list2))
       
   190             return false;
       
   191 
       
   192         return isTypeMatched(type1.getRowType(), type2.getRowType());
       
   193     }
       
   194 
       
   195     private static final long serialVersionUID = -2190411934472666714L;
   230     private static final long serialVersionUID = -2190411934472666714L;
   196 }
   231 }