001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package java.lang.management;
019:
020: import javax.management.openmbean.CompositeData;
021:
022: /**
023: * <p>
024: * A memory usage snapshot.
025: * </p>
026: *
027: * @since 1.5
028: */
029: public class MemoryUsage {
030:
031: /**
032: * <p>
033: * Constructs a MemoryUsage object from the CompositeData passed.
034: * </p>
035: *
036: * @param cd The CompositeDate object to retrieve data from.
037: * @return A MemoryUsage instance.
038: * @throws IllegalArgumentException if <code>cd</code> does not contain
039: * MemoryUsage data.
040: */
041: public static MemoryUsage from(CompositeData cd) {
042: try {
043: long init = ((Long) cd.get("init")).longValue();
044: long used = ((Long) cd.get("used")).longValue();
045: long committed = ((Long) cd.get("committed")).longValue();
046: long max = ((Long) cd.get("max")).longValue();
047: return new MemoryUsage(init, used, committed, max);
048: } catch (ClassCastException e) {
049: // if any cast fails, then a type was incorrect
050: throw new IllegalArgumentException(e);
051: }
052: }
053:
054: private final long init;
055:
056: private final long used;
057:
058: private final long committed;
059:
060: private final long max;
061:
062: /**
063: * <p>
064: * Constructs a new MemoryUsage instance.
065: * </p>
066: *
067: * @param init The initial amount of memory (bytes) or <code>-1</code> if
068: * undefined.
069: * @param used The amount of memory used (bytes).
070: * @param committed The amount of memory committed (bytes).
071: * @param max The maximum amount of memory available or <code>-1</code> if
072: * undefined.
073: * @throws IllegalArgumentException if <code>init</code> or
074: * <code>max</code> are less than -1, <code>used</code> or
075: * <code>committed</code> is negative, <code>used</code> is
076: * greater than <code>committed</code> or <code>committed</code>
077: * is greater than <code>max</code> if defined.
078: */
079: public MemoryUsage(long init, long used, long committed, long max) {
080: super ();
081: if (init < -1 || max < -1) {
082: throw new IllegalArgumentException();
083: }
084: if (used < 0 || committed < 0 || used > committed) {
085: throw new IllegalArgumentException();
086: }
087: if (max != -1 && committed > max) {
088: throw new IllegalArgumentException();
089: }
090: this .init = init;
091: this .used = used;
092: this .committed = committed;
093: this .max = max;
094: }
095:
096: public long getCommitted() {
097: return committed;
098: }
099:
100: public long getInit() {
101: return init;
102: }
103:
104: public long getMax() {
105: return max;
106: }
107:
108: public long getUsed() {
109: return used;
110: }
111:
112: @Override
113: public String toString() {
114: return "MemoryUsage: init=" + init + "used=" + used
115: + "committed=" + committed + "max=" + max;
116: }
117: }
|