alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (TabularType.java)

This example Java source code file (TabularType.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

argument\'s, classcastexception, compositetype, illegalargumentexception, integer, list, opendataexception, opentype, override, string, stringbuilder, tabulardata, tabulartype, util

The TabularType.java Java example source code

/*
 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */


package javax.management.openmbean;


// java import
//
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

// jmx import
//


/**
 * The <code>TabularType class is the  open type class
 * whose instances describe the types of {@link TabularData TabularData} values.
 *
 * @since 1.5
 */
public class TabularType extends OpenType<TabularData> {

    /* Serial version */
    static final long serialVersionUID = 6554071860220659261L;


    /**
     * @serial The composite type of rows
     */
    private CompositeType  rowType;

    /**
     * @serial The items used to index each row element, kept in the order the user gave
     *         This is an unmodifiable {@link ArrayList}
     */
    private List<String> indexNames;


    private transient Integer myHashCode = null; // As this instance is immutable, these two values
    private transient String  myToString = null; // need only be calculated once.


    /* *** Constructor *** */

    /**
     * Constructs a <code>TabularType instance, checking for the validity of the given parameters.
     * The validity constraints are described below for each parameter.
     * <p>
     * The Java class name of tabular data values this tabular type represents
     * (ie the class name returned by the {@link OpenType#getClassName() getClassName} method)
     * is set to the string value returned by <code>TabularData.class.getName().
     * <p>
     * @param  typeName  The name given to the tabular type this instance represents; cannot be a null or empty string.
     * <br> 
     * @param  description  The human readable description of the tabular type this instance represents;
     *                      cannot be a null or empty string.
     * <br> 
     * @param  rowType  The type of the row elements of tabular data values described by this tabular type instance;
     *                  cannot be null.
     * <br> 
     * @param  indexNames  The names of the items the values of which are used to uniquely index each row element in the
     *                     tabular data values described by this tabular type instance;
     *                     cannot be null or empty. Each element should be an item name defined in <var>rowType
     *                     (no null or empty string allowed).
     *                     It is important to note that the <b>order of the item names in indexNames
     *                     is used by the methods {@link TabularData#get(java.lang.Object[]) get} and
     *                     {@link TabularData#remove(java.lang.Object[]) remove} of class
     *                     <code>TabularData to match their array of values parameter to items.
     * <br> 
     * @throws IllegalArgumentException  if <var>rowType is null,
     *                                   or <var>indexNames is a null or empty array,
     *                                   or an element in <var>indexNames is a null or empty string,
     *                                   or <var>typeName or description is a null or empty string.
     * <br> 
     * @throws OpenDataException  if an element's value of <var>indexNames
     *                            is not an item name defined in <var>rowType.
     */
    public TabularType(String         typeName,
                       String         description,
                       CompositeType  rowType,
                       String[]       indexNames) throws OpenDataException {

        // Check and initialize state defined by parent.
        //
        super(TabularData.class.getName(), typeName, description, false);

        // Check rowType is not null
        //
        if (rowType == null) {
            throw new IllegalArgumentException("Argument rowType cannot be null.");
        }

        // Check indexNames is neither null nor empty and does not contain any null element or empty string
        //
        checkForNullElement(indexNames, "indexNames");
        checkForEmptyString(indexNames, "indexNames");

        // Check all indexNames values are valid item names for rowType
        //
        for (int i=0; i<indexNames.length; i++) {
            if ( ! rowType.containsKey(indexNames[i]) ) {
                throw new OpenDataException("Argument's element value indexNames["+ i +"]=\""+ indexNames[i] +
                                            "\" is not a valid item name for rowType.");
            }
        }

        // initialize rowType
        //
        this.rowType    = rowType;

        // initialize indexNames (copy content so that subsequent
        // modifs to the array referenced by the indexNames parameter
        // have no impact)
        //
        List<String> tmpList = new ArrayList(indexNames.length + 1);
        for (int i=0; i<indexNames.length; i++) {
            tmpList.add(indexNames[i]);
        }
        this.indexNames = Collections.unmodifiableList(tmpList);
    }

    /**
     * Checks that Object[] arg is neither null nor empty (ie length==0)
     * and that it does not contain any null element.
     */
    private static void checkForNullElement(Object[] arg, String argName) {
        if ( (arg == null) || (arg.length == 0) ) {
            throw new IllegalArgumentException("Argument "+ argName +"[] cannot be null or empty.");
        }
        for (int i=0; i<arg.length; i++) {
            if (arg[i] == null) {
                throw new IllegalArgumentException("Argument's element "+ argName +"["+ i +"] cannot be null.");
            }
        }
    }

    /**
     * Checks that String[] does not contain any empty (or blank characters only) string.
     */
    private static void checkForEmptyString(String[] arg, String argName) {
        for (int i=0; i<arg.length; i++) {
            if (arg[i].trim().equals("")) {
                throw new IllegalArgumentException("Argument's element "+ argName +"["+ i +"] cannot be an empty string.");
            }
        }
    }


    /* *** Tabular type specific information methods *** */

    /**
     * Returns the type of the row elements of tabular data values
     * described by this <code>TabularType instance.
     *
     * @return the type of each row.
     */
    public CompositeType getRowType() {

        return rowType;
    }

    /**
     * <p>Returns, in the same order as was given to this instance's
     * constructor, an unmodifiable List of the names of the items the
     * values of which are used to uniquely index each row element of
     * tabular data values described by this <code>TabularType
     * instance.</p>
     *
     * @return a List of String representing the names of the index
     * items.
     *
     */
    public List<String> getIndexNames() {

        return indexNames;
    }

    /**
     * Tests whether <var>obj is a value which could be
     * described by this <code>TabularType instance.
     *
     * <p>If obj is null or is not an instance of
     * <code>javax.management.openmbean.TabularData,
     * <code>isValue returns false.

* * <p>If obj is an instance of * <code>javax.management.openmbean.TabularData, say {@code * td}, the result is true if this {@code TabularType} is * <em>assignable from {@link TabularData#getTabularType() * td.getTabularType()}, as defined in {@link * CompositeType#isValue CompositeType.isValue}.</p> * * @param obj the value whose open type is to be tested for * compatibility with this <code>TabularType instance. * * @return <code>true if obj is a value for this * tabular type, <code>false otherwise. */ public boolean isValue(Object obj) { // if obj is null or not a TabularData, return false // if (!(obj instanceof TabularData)) return false; // if obj is not a TabularData, return false // TabularData value = (TabularData) obj; TabularType valueType = value.getTabularType(); return isAssignableFrom(valueType); } @Override boolean isAssignableFrom(OpenType<?> ot) { if (!(ot instanceof TabularType)) return false; TabularType tt = (TabularType) ot; if (!getTypeName().equals(tt.getTypeName()) || !getIndexNames().equals(tt.getIndexNames())) return false; return getRowType().isAssignableFrom(tt.getRowType()); } /* *** Methods overriden from class Object *** */ /** * Compares the specified <code>obj parameter with this TabularType instance for equality. * <p> * Two <code>TabularType instances are equal if and only if all of the following statements are true: * <ul> * <li>their type names are equal * <li>their row types are equal * <li>they use the same index names, in the same order * </ul> * <br>  * @param obj the object to be compared for equality with this <code>TabularType instance; * if <var>obj is null, equals returns false. * * @return <code>true if the specified object is equal to this TabularType instance. */ public boolean equals(Object obj) { // if obj is null, return false // if (obj == null) { return false; } // if obj is not a TabularType, return false // TabularType other; try { other = (TabularType) obj; } catch (ClassCastException e) { return false; } // Now, really test for equality between this TabularType instance and the other: // // their names should be equal if ( ! this.getTypeName().equals(other.getTypeName()) ) { return false; } // their row types should be equal if ( ! this.rowType.equals(other.rowType) ) { return false; } // their index names should be equal and in the same order (ensured by List.equals()) if ( ! this.indexNames.equals(other.indexNames) ) { return false; } // All tests for equality were successfull // return true; } /** * Returns the hash code value for this <code>TabularType instance. * <p> * The hash code of a <code>TabularType instance is the sum of the hash codes * of all elements of information used in <code>equals comparisons * (ie: name, row type, index names). * This ensures that <code> t1.equals(t2) implies that t1.hashCode()==t2.hashCode() * for any two <code>TabularType instances t1 and t2, * as required by the general contract of the method * {@link Object#hashCode() Object.hashCode()}. * <p> * As <code>TabularType instances are immutable, the hash code for this instance is calculated once, * on the first call to <code>hashCode, and then the same value is returned for subsequent calls. * * @return the hash code value for this <code>TabularType instance */ public int hashCode() { // Calculate the hash code value if it has not yet been done (ie 1st call to hashCode()) // if (myHashCode == null) { int value = 0; value += this.getTypeName().hashCode(); value += this.rowType.hashCode(); for (String index : indexNames) value += index.hashCode(); myHashCode = Integer.valueOf(value); } // return always the same hash code for this instance (immutable) // return myHashCode.intValue(); } /** * Returns a string representation of this <code>TabularType instance. * <p> * The string representation consists of the name of this class (ie <code>javax.management.openmbean.TabularType), * the type name for this instance, the row type string representation of this instance, * and the index names of this instance. * <p> * As <code>TabularType instances are immutable, the string representation for this instance is calculated once, * on the first call to <code>toString, and then the same value is returned for subsequent calls. * * @return a string representation of this <code>TabularType instance */ public String toString() { // Calculate the string representation if it has not yet been done (ie 1st call to toString()) // if (myToString == null) { final StringBuilder result = new StringBuilder() .append(this.getClass().getName()) .append("(name=") .append(getTypeName()) .append(",rowType=") .append(rowType.toString()) .append(",indexNames=("); String sep = ""; for (String index : indexNames) { result.append(sep).append(index); sep = ","; } result.append("))"); myToString = result.toString(); } // return always the same string representation for this instance (immutable) // return myToString; } }

Other Java examples (source code examples)

Here is a short list of links related to this Java TabularType.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.