1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  *
26  * ident	"%Z%%M%	%I%	%E% SMI"
27  */
28 package org.opensolaris.os.dtrace;
29 
30 import java.io.*;
31 import java.beans.*;
32 
33 /**
34  * A {@code long} value aggregated by the DTrace {@code count()} action.
35  * <p>
36  * Immutable.  Supports persistence using {@link java.beans.XMLEncoder}.
37  *
38  * @see Aggregation
39  * @author Tom Erickson
40  */
41 public final class CountValue extends AbstractAggregationValue {
42     static final long serialVersionUID = 5948954123445410783L;
43 
44     static {
45 	try {
46 	    BeanInfo info = Introspector.getBeanInfo(CountValue.class);
47 	    PersistenceDelegate persistenceDelegate =
48 		    new DefaultPersistenceDelegate(
49 		    new String[] {"value"});
50 	    BeanDescriptor d = info.getBeanDescriptor();
51 	    d.setValue("persistenceDelegate", persistenceDelegate);
52 	} catch (IntrospectionException e) {
53 	    System.out.println(e);
54 	}
55     }
56 
57     /**
58      * Creates a value aggregated by the DTrace {@code count()} action.
59      * Supports XML persistence.
60      *
61      * @param v aggregated value count
62      * @throws IllegalArgumentException if the given count is negative
63      */
64     public
65     CountValue(long v)
66     {
67 	super(v);
68 	validate();
69     }
70 
71     private final void
72     validate()
73     {
74 	long count = super.getValue().longValue();
75 	if (count < 0) {
76 	    throw new IllegalArgumentException("count is negative");
77 	}
78     }
79 
80     // Needed to support XML persistence since XMLDecoder cannot find
81     // the public method of the non-public superclass.
82 
83     /**
84      * Gets the number of aggregated values.
85      *
86      * @return the number of aggregated values
87      */
88     public Long
89     getValue()
90     {
91 	return (Long)super.getValue();
92     }
93 
94     private void
95     readObject(ObjectInputStream s)
96             throws IOException, ClassNotFoundException
97     {
98 	s.defaultReadObject();
99 	// check invariants
100 	try {
101 	    validate();
102 	} catch (Exception e) {
103 	    throw new InvalidObjectException(e.getMessage());
104 	}
105     }
106 }
107