1 /*
2 * Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package java.lang;
26
27 /**
28 * This is the common base class of all Java language record classes.
29 *
30 * <p>More information about records, including descriptions of the
31 * implicitly declared methods synthesized by the compiler, can be
32 * found in section 8.10 of
33 * <cite>The Java Language Specification</cite>.
34 *
35 * <p>A <em>record class</em> is a shallowly immutable, transparent carrier for
36 * a fixed set of values, called the <em>record components</em>. The Java
37 * language provides concise syntax for declaring record classes, whereby the
38 * record components are declared in the record header. The list of record
39 * components declared in the record header form the <em>record descriptor</em>.
40 *
41 * <p>A record class has the following mandated members: a <em>canonical
42 * constructor</em>, which must provide at least as much access as the record
43 * class and whose descriptor is the same as the record descriptor;
44 * a private final field corresponding to each component, whose name and
45 * type are the same as that of the component; a public accessor method
46 * corresponding to each component, whose name and return type are the same as
47 * that of the component. If not explicitly declared in the body of the record,
48 * implicit implementations for these members are provided.
49 *
50 * <p>The implicit declaration of the canonical constructor has the same accessibility
51 * as the record class and initializes the component fields from the corresponding
52 * constructor arguments. The implicit declaration of the accessor methods returns
53 * the value of the corresponding component field. The implicit declaration of the
54 * {@link Object#equals(Object)}, {@link Object#hashCode()}, and {@link Object#toString()}
55 * methods are derived from all of the component fields.
56 *
57 * <p>The primary reasons to provide an explicit declaration for the
58 * canonical constructor or accessor methods are to validate constructor
59 * arguments, perform defensive copies on mutable components, or normalize groups
60 * of components (such as reducing a rational number to lowest terms.)
61 *
62 * <p>For all record classes, the following invariant must hold: if a record R's
63 * components are {@code c1, c2, ... cn}, then if a record instance is copied
64 * as follows:
65 * <pre>
66 * R copy = new R(r.c1(), r.c2(), ..., r.cn());
67 * </pre>
68 * then it must be the case that {@code r.equals(copy)}.
69 *
70 * <div class="preview-block">
71 * <div class="preview-comment">
72 * When preview features are enabled, {@code Record} is
73 * an abstract {@linkplain Class#isValue value class}.
74 * Subclasses of {@code Record} can be either an {@linkplain Class#isIdentity identity class}
75 * or a {@linkplain Class#isValue value class}.
76 * See {@jls The Java Language Specification 8.1.1.5 Value Classes}.
77 * </div>
78 * </div>
79 *
80 * @apiNote
81 * A record class that {@code implements} {@link java.io.Serializable} is said
82 * to be a <i>serializable record</i>. Serializable records are serialized and
83 * deserialized differently than ordinary serializable objects. During
84 * deserialization the record's canonical constructor is invoked to construct
85 * the record object. Certain serialization-related methods, such as readObject
86 * and writeObject, are ignored for serializable records. More information about
87 * serializable records can be found in the
88 * <a href="{@docRoot}/../specs/serialization/serial-arch.html#serialization-of-records">
89 * <cite>Java Object Serialization Specification,</cite> Section 1.13,
90 * "Serialization of Records"</a>.
91 *
92 * @apiNote
93 * A record class structure can be obtained at runtime via reflection.
94 * See {@link Class#isRecord()} and {@link Class#getRecordComponents()} for more details.
95 *
96 * @spec serialization/index.html Java Object Serialization Specification
97 * @jls 8.10 Record Classes
98 * @since 16
99 */
100 @jdk.internal.MigratedValueClass
101 @jdk.internal.ValueBased
102 public abstract class Record {
103 /**
104 * Constructor for record classes to call.
105 */
106 protected Record() {}
107
108 /**
109 * Indicates whether some other object is "equal to" this one. In addition
110 * to the general contract of {@link Object#equals(Object) Object.equals},
111 * record classes must further obey the invariant that when
112 * a record instance is "copied" by passing the result of the record component
113 * accessor methods to the canonical constructor, as follows:
114 * <pre>
115 * R copy = new R(r.c1(), r.c2(), ..., r.cn());
116 * </pre>
117 * then it must be the case that {@code r.equals(copy)}.
118 *
119 * @implSpec
120 * The implicitly provided implementation returns {@code true} if
121 * and only if the argument is an instance of the same record class
122 * as this record, and each component of this record is equal to
123 * the corresponding component of the argument; otherwise, {@code
124 * false} is returned. Equality of a component {@code c} is
125 * determined as follows:
126 * <ul>
127 *
128 * <li> If the component is of a reference type, the component is
129 * considered equal if and only if {@link
130 * java.util.Objects#equals(Object,Object)
131 * Objects.equals(this.c, r.c)} would return {@code true}.
132 *
133 * <li> If the component is of a primitive type, using the
134 * corresponding primitive wrapper class {@code PW} (the
135 * corresponding wrapper class for {@code int} is {@code
136 * java.lang.Integer}, and so on), the component is considered
137 * equal if and only if {@code
138 * PW.compare(this.c, r.c)} would return {@code 0}.
139 *
140 * </ul>
141 *
142 * Note that these rules imply that {@linkplain
143 * Double##repEquivalence representation equivalence} is used for
144 * the equality comparison of both primitive floating-point values
145 * and wrapped floating-point values.
146 *
147 * <p>Apart from the semantics described above, the precise algorithm
148 * used in the implicitly provided implementation is unspecified
149 * and is subject to change. The implementation may or may not use
150 * calls to the particular methods listed, and may or may not
151 * perform comparisons in the order of component declaration.
152 *
153 * @see java.util.Objects#equals(Object,Object)
154 *
155 * @param obj the reference object with which to compare.
156 * @return {@code true} if this record is equal to the
157 * argument; {@code false} otherwise.
158 */
159 @Override
160 public abstract boolean equals(Object obj);
161
162 /**
163 * Returns a hash code value for the record.
164 * Obeys the general contract of {@link Object#hashCode Object.hashCode}.
165 * For records, hashing behavior is constrained by the refined contract
166 * of {@link Record#equals Record.equals}, so that any two records
167 * created from the same components must have the same hash code.
168 *
169 * @implSpec
170 * The implicitly provided implementation returns a hash code value derived
171 * by combining appropriate hashes from each component.
172 * The precise algorithm used in the implicitly provided implementation
173 * is unspecified and is subject to change within the above limits.
174 * The resulting integer need not remain consistent from one
175 * execution of an application to another execution of the same
176 * application, even if the hashes of the component values were to
177 * remain consistent in this way. Also, a component of primitive
178 * type may contribute its bits to the hash code differently than
179 * the {@code hashCode} of its primitive wrapper class.
180 *
181 * @see Object#hashCode()
182 *
183 * @return a hash code value for this record.
184 */
185 @Override
186 public abstract int hashCode();
187
188 /**
189 * Returns a string representation of the record.
190 * In accordance with the general contract of {@link Object#toString()},
191 * the {@code toString} method returns a string that
192 * "textually represents" this record. The result should
193 * be a concise but informative representation that is easy for a
194 * person to read.
195 * <p>
196 * In addition to this general contract, record classes must further
197 * participate in the invariant that any two records which are
198 * {@linkplain Record#equals(Object) equal} must produce equal
199 * strings. This invariant is necessarily relaxed in the rare
200 * case where corresponding equal component values might fail
201 * to produce equal strings for themselves.
202 *
203 * @implSpec
204 * The implicitly provided implementation returns a string which
205 * contains the name of the record class, the names of components
206 * of the record, and string representations of component values,
207 * so as to fulfill the contract of this method.
208 * The precise format produced by this implicitly provided implementation
209 * is subject to change, so the present syntax should not be parsed
210 * by applications to recover record component values.
211 *
212 * @see Object#toString()
213 *
214 * @return a string representation of the object.
215 */
216 @Override
217 public abstract String toString();
218 }