Resource Management is a commercial feature that must be unlocked before being used. To learn more about commercial features and how to unlock them visit http://www.oracle.com/technetwork/java/javaseproducts/
jdk.management.resource

## Class SimpleMeter

- java.lang.Object
- - jdk.management.resource.SimpleMeter

- All Implemented Interfaces:
    ResourceMeter, ResourceRequest
  Direct Known Subclasses:
    NotifyingMeter

      public class SimpleMeter
      extends Object
      implements ResourceMeter, ResourceRequest

  A SimpleMeter counts resource requests and releases and allocates from an optional parent. The amount is validated before requesting allocation from the parent. Subclasses can override the validation to include bounds checking, notification, callbacks, etc.
  Since:
    8u40

- - ### Constructor Summary

    Constructors
    Modifier | Constructor and Description
    --- | ---
    `protected` | `SimpleMeter(ResourceType type, ResourceRequest parent)` Constructor for subclasses.

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `static SimpleMeter` | `create(ResourceType type)` Returns a new SimpleMeter with the ResourceType.
    `static SimpleMeter` | `create(ResourceType type, ResourceRequest parent)` Returns a new SimpleMeter with the ResourceType and allocating from a parent.
    `boolean` | `equals(Object obj)` Indicates whether some other object is the same object as this one.
    `long` | `getAllocated()` Returns the cumulative total of allocations from the ResourceMeter.
    `ResourceRequest` | `getParent()` Returns the parent ResourceRequest.
    `ResourceType` | `getType()` Returns the `ResourceType`.
    `long` | `getValue()` Returns the current net value of the ResourceMeter.
    `int` | `hashCode()` Returns the hashcode from for this object.
    `long` | `request(long amount, ResourceId id)` Returns the validated and allocated amount and adjusts the meter.
    `String` | `toString()` Returns a string representation of the meter.
    `protected long` | `validate(long previous, long amount, ResourceId id)` Validate the amount returning the validated amount.

    - ### Methods inherited from class java.lang.Object

      `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

- - ### Constructor Detail

    - #### SimpleMeter

          protected SimpleMeter(ResourceType type,
                                ResourceRequest parent)

      Constructor for subclasses.
      Parameters:
        `type` - the ResourceType for the meter
        `parent` - a ResourceRequest used as the parent to allocate from; may be `null`

  - ### Method Detail

    - #### create

          public static SimpleMeter create(ResourceType type)

      Returns a new SimpleMeter with the ResourceType.
      Parameters:
        `type` - the ResourceType
      Returns:
        a new SimpleMeter

    - #### create

          public static SimpleMeter create(ResourceType type,
                                           ResourceRequest parent)

      Returns a new SimpleMeter with the ResourceType and allocating from a parent.
      Parameters:
        `type` - the ResourceType
        `parent` - the parent ResourceMeter; may be `null`
      Returns:
        a new SimpleMeter

    - #### getValue

          public final long getValue()

      Description copied from interface: `ResourceMeter`
      Returns the current net value of the ResourceMeter. The net value is the difference between the cumulative amounts allocated minus all amounts released.
      Specified by:
        `getValue` in interface `ResourceMeter`
      Returns:
        the current net value of the meter

    - #### getAllocated

          public final long getAllocated()

      Description copied from interface: `ResourceMeter`
      Returns the cumulative total of allocations from the ResourceMeter.
      Specified by:
        `getAllocated` in interface `ResourceMeter`
      Returns:
        the total allocated from the meter

    - #### getType

          public final ResourceType getType()

      Description copied from interface: `ResourceMeter`
      Returns the `ResourceType`.
      Specified by:
        `getType` in interface `ResourceMeter`
      Returns:
        the ResourceType

    - #### getParent

          public final ResourceRequest getParent()

      Returns the parent ResourceRequest.
      Returns:
        Returns the parent ResourceRequest; may be null

    - #### request

          public final long request(long amount,
                                    ResourceId id)

      Returns the validated and allocated amount and adjusts the meter. For allocation, amount greater than zero, the amount is applied to the meter before validation so it is immediately visible. If the validation or allocation from the parent is for a different amount the delta is restored to the meter. For releasing a resource, amount less than zero, the amount is applied after validation to avoid premature release of the resource.
      Specified by:
        `request` in interface `ResourceRequest`
      Parameters:
        `amount` - the amount to allocate; if zero nothing is done
        `id` - the ResourceId for the resource instance; may be `null`
      Returns:
        the allocated amount, zero, positive or negative

    - #### validate

          protected long validate(long previous,
                                  long amount,
                                  ResourceId id)
                           throws ResourceRequestDeniedException

      Validate the amount returning the validated amount. Override to apply bounds checks and callbacks.

      For allocation, positive amounts, the amount is applied to the meter before validation so it is immediately visible. For releasing a resource, negative amounts, the amount is applied after validation to avoid premature release of the resource. If the amount returned is different than requested the meter will be corrected with the difference.

      Parameters:
        `previous` - the meter value before the current request
        `amount` - an amount, positive to allocate, negative to release
        `id` - the ResourceId for the resource instance; may be `null`
      Returns:
        the validated amount, zero or throw an exception to deny; return a value with the same sign as the `amount`
      Throws:
        `ResourceRequestDeniedException` - if the amount cannot be satisfied

    - #### toString

          public String toString()

      Returns a string representation of the meter.
      Overrides:
        `toString` in class `Object`
      Returns:
        a string representation of the meter

    - #### hashCode

          public final int hashCode()

      Returns the hashcode from for this object.
      Overrides:
        `hashCode` in class `Object`
      Returns:
        a hashcode for this object
      See Also:
        `Object.equals(java.lang.Object)`, `System.identityHashCode(java.lang.Object)`

    - #### equals

          public final boolean equals(Object obj)

      Indicates whether some other object is the same object as this one.
      Overrides:
        `equals` in class `Object`
      Parameters:
        `obj` - the reference object with which to compare
      Returns:
        `true` if this object is the same as the obj argument; `false` otherwise
      See Also:
        `Object.hashCode()`, `HashMap`

Copyright © 2014, 2016, Oracle and/or its affiliates. All rights reserved.