Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/management/LazyCompositeData.java
38827 views
/*1* Copyright (c) 2004, 2012, 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 {4748private CompositeData compositeData;4950// Implementation of the CompositeData interface51@Override52public boolean containsKey(String key) {53return compositeData().containsKey(key);54}5556@Override57public boolean containsValue(Object value) {58return compositeData().containsValue(value);59}6061@Override62public boolean equals(Object obj) {63return compositeData().equals(obj);64}6566@Override67public Object get(String key) {68return compositeData().get(key);69}7071@Override72public Object[] getAll(String[] keys) {73return compositeData().getAll(keys);74}7576@Override77public CompositeType getCompositeType() {78return compositeData().getCompositeType();79}8081@Override82public int hashCode() {83return compositeData().hashCode();84}8586@Override87public String toString() {88/** FIXME: What should this be?? */89return compositeData().toString();90}9192@Override93public Collection<?> values() {94return compositeData().values();95}9697/* Lazy creation of a CompositeData object98* only when the CompositeData interface is used.99*/100private synchronized CompositeData compositeData() {101if (compositeData != null)102return compositeData;103compositeData = getCompositeData();104return compositeData;105}106107/**108* Designate to a CompositeData object when writing to an109* output stream during serialization so that the receiver110* only requires JMX 1.2 classes but not any implementation111* specific class.112*/113protected Object writeReplace() throws java.io.ObjectStreamException {114return compositeData();115}116117/**118* Returns the CompositeData representing this object.119* The returned CompositeData object must be an instance120* of javax.management.openmbean.CompositeDataSupport class121* so that no implementation specific class is required122* for unmarshalling besides JMX 1.2 classes.123*/124protected abstract CompositeData getCompositeData();125126// Helper methods127static String getString(CompositeData cd, String itemName) {128if (cd == null)129throw new IllegalArgumentException("Null CompositeData");130131return (String) cd.get(itemName);132}133134static boolean getBoolean(CompositeData cd, String itemName) {135if (cd == null)136throw new IllegalArgumentException("Null CompositeData");137138return ((Boolean) cd.get(itemName));139}140141static long getLong(CompositeData cd, String itemName) {142if (cd == null)143throw new IllegalArgumentException("Null CompositeData");144145return ((Long) cd.get(itemName));146}147148static int getInt(CompositeData cd, String itemName) {149if (cd == null)150throw new IllegalArgumentException("Null CompositeData");151152return ((Integer) cd.get(itemName));153}154155/**156* Compares two CompositeTypes and returns true if157* all items in type1 exist in type2 and their item types158* are the same.159* @param type1 the base composite type160* @param type2 the checked composite type161* @return {@code true} if all items in type1 exist in type2 and their item162* types are the same.163*/164protected static boolean isTypeMatched(CompositeType type1, CompositeType type2) {165if (type1 == type2) return true;166167// We can't use CompositeType.isValue() since it returns false168// if the type name doesn't match.169Set<String> allItems = type1.keySet();170171// Check all items in the type1 exist in type2172if (!type2.keySet().containsAll(allItems))173return false;174175return allItems.stream().allMatch(176item -> isTypeMatched(type1.getType(item), type2.getType(item))177);178}179180protected static boolean isTypeMatched(TabularType type1, TabularType type2) {181if (type1 == type2) return true;182183List<String> list1 = type1.getIndexNames();184List<String> list2 = type2.getIndexNames();185186// check if the list of index names are the same187if (!list1.equals(list2))188return false;189190return isTypeMatched(type1.getRowType(), type2.getRowType());191}192193protected static boolean isTypeMatched(ArrayType<?> type1, ArrayType<?> type2) {194if (type1 == type2) return true;195196int dim1 = type1.getDimension();197int dim2 = type2.getDimension();198199// check if the array dimensions are the same200if (dim1 != dim2)201return false;202203return isTypeMatched(type1.getElementOpenType(), type2.getElementOpenType());204}205206private static boolean isTypeMatched(OpenType<?> ot1, OpenType<?> ot2) {207if (ot1 instanceof CompositeType) {208if (! (ot2 instanceof CompositeType))209return false;210if (!isTypeMatched((CompositeType) ot1, (CompositeType) ot2))211return false;212} else if (ot1 instanceof TabularType) {213if (! (ot2 instanceof TabularType))214return false;215if (!isTypeMatched((TabularType) ot1, (TabularType) ot2))216return false;217} else if (ot1 instanceof ArrayType) {218if (! (ot2 instanceof ArrayType))219return false;220if (!isTypeMatched((ArrayType<?>) ot1, (ArrayType<?>) ot2)) {221return false;222}223} else if (!ot1.equals(ot2)) {224return false;225}226return true;227}228229private static final long serialVersionUID = -2190411934472666714L;230}231232233