Record Class PrivateData

java.lang.Object
java.lang.Record
org.jhotdraw8.icollection.PrivateData

public record PrivateData(@Nullable Object object) extends Record
This record holds an object that contains private data.

This record is used to allow subclassing of immutable classes that need to create new instances of subclasses, but that do not want to export their internal data structures.

Example:

The module 'foo' exports the package that contains class ImmutableFoo. The module 'foo' does not export the package that contains InternalHashtable used by ImmutableFoo.

To allow subclassing of ImmutableFoo by other modules, class ImmutableFoo provides a protected constructor and a protected newInstance method that take an PrivateData object as parameters.

 module org.foo {
     exports org.foo;
 }
 package org.foo.impl;

import java.util.logging.Level;
import java.util.logging.Logger;
 public class InternalHashtable {
    ...
 }
 package org.foo;

import java.util.logging.Level;
import java.util.logging.Logger;
 public class ImmutableFoo {
     private final InternalHashtable table;
     public ImmutableFoo() {
        table = new InternalHashtable();
     }
     protected ImmutableFoo(PrivateData opaque) {
         this.table = opaque.get();
     }
     protected ImmutableFoo newInstance(PrivateData opaque) {
         return new ImmutableFoo(opaque);
     }
 }


 module org.bar {
     imports org.foo;
     exports org.bar;
 }
 package org.bar;

import java.util.logging.Level;
import java.util.logging.Logger;
 public class ImmutableBar extends ImmutableFoo {
     private final int bar;
     public ImmutableBar(int bar) {
         this.bar=bar;
     }
     protected ImmutableBar(PrivateData opaque, int bar) {
         super(opaque);
         this.bar=bar;
     }
     protected newInstance(PrivateData opaque) {
         return new ImmutableBar(opaque,this.bar);
     }
 }
 
  • Constructor Summary

    Constructors
    Constructor
    Description
    PrivateData(@Nullable Object object)
    Creates an instance of a PrivateData record class.
  • Method Summary

    Modifier and Type
    Method
    Description
    final boolean
    Indicates whether some other object is "equal to" this one.
    final int
    Returns a hash code value for this object.
    @Nullable Object
    Returns the value of the object record component.
    final String
    Returns a string representation of this record class.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • PrivateData

      public PrivateData(@Nullable Object object)
      Creates an instance of a PrivateData record class.
      Parameters:
      object - the value for the object record component
  • Method Details

    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • object

      public @Nullable Object object()
      Returns the value of the object record component.
      Returns:
      the value of the object record component