1 /*
2  * Copyright 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.xr.compose.subspace.node
18 
19 import androidx.annotation.RestrictTo
20 import androidx.xr.compose.subspace.layout.SubspaceModifier
21 
22 /**
23  * Modifier elements manage an instance of a particular [SubspaceModifier.Node] implementation. A
24  * given [SubspaceModifier.Node] implementation can only be used when a
25  * [SubspaceModifierNodeElement], which creates and updates that implementation, is applied to a
26  * layout.
27  *
28  * A [SubspaceModifierNodeElement] should be very lightweight, and do little more than hold the
29  * information necessary to create and maintain an instance of the associated
30  * [SubspaceModifier.Node] type.
31  *
32  * @param N The type of node that this element creates and updates.
33  */
34 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP_PREFIX)
35 public abstract class SubspaceModifierNodeElement<N : SubspaceModifier.Node> : SubspaceModifier {
36     /**
37      * This will be called the first time the modifier is applied to the layout and it should
38      * construct and return the corresponding [SubspaceModifier.Node] instance.
39      */
createnull40     public abstract fun create(): N
41 
42     /**
43      * Called when a modifier is applied to a layout whose inputs have changed from the previous
44      * application. This function will have the current node instance passed in as a parameter, and
45      * it is expected that the node will be brought up to date.
46      */
47     public abstract fun update(node: N)
48 
49     /**
50      * Require hashCode() to be implemented. Using a data class is sufficient. Singletons and
51      * modifiers with no parameters may implement this function by returning an arbitrary constant.
52      */
53     public abstract override fun hashCode(): Int
54 
55     /**
56      * Require equals() to be implemented. Using a data class is sufficient. Singletons may
57      * implement this function with referential equality (`this === other`). Modifiers with no
58      * inputs may implement this function by checking the type of the other object.
59      */
60     public abstract override fun equals(other: Any?): Boolean
61 }
62