Path: blob/master/src/java.management/share/classes/sun/management/LazyCompositeData.java
41152 views
/*1* Copyright (c) 2004, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.management;2627import java.io.Serializable;28import java.util.*;29import javax.management.openmbean.ArrayType;30import javax.management.openmbean.CompositeData;31import javax.management.openmbean.CompositeType;32import javax.management.openmbean.OpenType;33import javax.management.openmbean.TabularType;3435/**36* This abstract class provides the implementation of the CompositeData37* interface. A CompositeData object will be lazily created only when38* the CompositeData interface is used.39*40* Classes that extends this abstract class will implement the41* getCompositeData() method. The object returned by the42* getCompositeData() is an instance of CompositeData such that43* the instance serializes itself as the type CompositeDataSupport.44*/45public abstract class LazyCompositeData46implements CompositeData, Serializable {4748@SuppressWarnings("serial") // Not statically typed as Serializable49private CompositeData compositeData;5051// Implementation of the CompositeData interface52@Override53public boolean containsKey(String key) {54return compositeData().containsKey(key);55}5657@Override58public boolean containsValue(Object value) {59return compositeData().containsValue(value);60}6162@Override63public boolean equals(Object obj) {64return compositeData().equals(obj);65}6667@Override68public Object get(String key) {69return compositeData().get(key);70}7172@Override73public Object[] getAll(String[] keys) {74return compositeData().getAll(keys);75}7677@Override78public CompositeType getCompositeType() {79return compositeData().getCompositeType();80}8182@Override83public int hashCode() {84return compositeData().hashCode();85}8687@Override88public String toString() {89/** FIXME: What should this be?? */90return compositeData().toString();91}9293@Override94public Collection<?> values() {95return compositeData().values();96}9798/* Lazy creation of a CompositeData object99* only when the CompositeData interface is used.100*/101private synchronized CompositeData compositeData() {102if (compositeData != null)103return compositeData;104compositeData = getCompositeData();105return compositeData;106}107108/**109* Designate to a CompositeData object when writing to an110* output stream during serialization so that the receiver111* only requires JMX 1.2 classes but not any implementation112* specific class.113*/114protected Object writeReplace() throws java.io.ObjectStreamException {115return compositeData();116}117118/**119* Returns the CompositeData representing this object.120* The returned CompositeData object must be an instance121* of javax.management.openmbean.CompositeDataSupport class122* so that no implementation specific class is required123* for unmarshalling besides JMX 1.2 classes.124*/125protected abstract CompositeData getCompositeData();126127// Helper methods128public static String getString(CompositeData cd, String itemName) {129if (cd == null)130throw new IllegalArgumentException("Null CompositeData");131132return (String) cd.get(itemName);133}134135public static boolean getBoolean(CompositeData cd, String itemName) {136if (cd == null)137throw new IllegalArgumentException("Null CompositeData");138139return ((Boolean) cd.get(itemName));140}141142public static long getLong(CompositeData cd, String itemName) {143if (cd == null)144throw new IllegalArgumentException("Null CompositeData");145146return ((Long) cd.get(itemName));147}148149public static int getInt(CompositeData cd, String itemName) {150if (cd == null)151throw new IllegalArgumentException("Null CompositeData");152153return ((Integer) cd.get(itemName));154}155156/**157* Compares two CompositeTypes and returns true if158* all items in type1 exist in type2 and their item types159* are the same.160* @param type1 the base composite type161* @param type2 the checked composite type162* @return {@code true} if all items in type1 exist in type2 and their item163* types are the same.164*/165protected static boolean isTypeMatched(CompositeType type1, CompositeType type2) {166if (type1 == type2) return true;167168// We can't use CompositeType.isValue() since it returns false169// if the type name doesn't match.170Set<String> allItems = type1.keySet();171172// Check all items in the type1 exist in type2173if (!type2.keySet().containsAll(allItems))174return false;175176return allItems.stream().allMatch(177item -> isTypeMatched(type1.getType(item), type2.getType(item))178);179}180181protected static boolean isTypeMatched(TabularType type1, TabularType type2) {182if (type1 == type2) return true;183184List<String> list1 = type1.getIndexNames();185List<String> list2 = type2.getIndexNames();186187// check if the list of index names are the same188if (!list1.equals(list2))189return false;190191return isTypeMatched(type1.getRowType(), type2.getRowType());192}193194protected static boolean isTypeMatched(ArrayType<?> type1, ArrayType<?> type2) {195if (type1 == type2) return true;196197int dim1 = type1.getDimension();198int dim2 = type2.getDimension();199200// check if the array dimensions are the same201if (dim1 != dim2)202return false;203204return isTypeMatched(type1.getElementOpenType(), type2.getElementOpenType());205}206207private static boolean isTypeMatched(OpenType<?> ot1, OpenType<?> ot2) {208if (ot1 instanceof CompositeType) {209if (! (ot2 instanceof CompositeType))210return false;211if (!isTypeMatched((CompositeType) ot1, (CompositeType) ot2))212return false;213} else if (ot1 instanceof TabularType) {214if (! (ot2 instanceof TabularType))215return false;216if (!isTypeMatched((TabularType) ot1, (TabularType) ot2))217return false;218} else if (ot1 instanceof ArrayType) {219if (! (ot2 instanceof ArrayType))220return false;221if (!isTypeMatched((ArrayType<?>) ot1, (ArrayType<?>) ot2)) {222return false;223}224} else if (!ot1.equals(ot2)) {225return false;226}227return true;228}229230private static final long serialVersionUID = -2190411934472666714L;231}232233234