• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 android.healthconnect.cts.phr.utils;
18 
19 import org.json.JSONException;
20 import org.json.JSONObject;
21 
22 /**
23  * Abstract super class for helper classes for building FHIR resources for testing.
24  *
25  * <p>Subclasses should implement {@link #returnThis()} as:
26  *
27  * <pre>{@code
28  * @Override
29  * protected XxxBuilder returnThis() {
30  *   return this;
31  * }
32  * }</pre>
33  *
34  * This enables the type information to work correctly.
35  *
36  * @param <T> the class of the subclass which is the concrete class.
37  */
38 abstract class FhirResourceBuilder<T extends FhirResourceBuilder<T>> {
39     private final JSONObject mFhir;
40 
FhirResourceBuilder(String json)41     FhirResourceBuilder(String json) {
42         try {
43             this.mFhir = new JSONObject(json);
44         } catch (JSONException e) {
45             throw new IllegalArgumentException(e);
46         }
47     }
48 
49     /**
50      * Help the subclasses return the correct type.
51      *
52      * @return the current object, correctly typed.
53      */
returnThis()54     protected abstract T returnThis();
55 
56     /**
57      * Set the FHIR id for the resource.
58      *
59      * @return this Builder.
60      */
setId(String id)61     public T setId(String id) {
62         return set("id", id);
63     }
64 
65     /**
66      * Sets an arbitrary String or JSON Object element in the FHIR resource.
67      *
68      * @param field the element to set.
69      * @param value the value to set
70      * @return this builder
71      */
set(String field, Object value)72     public T set(String field, Object value) {
73         try {
74             mFhir.put(field, value);
75         } catch (JSONException e) {
76             throw new IllegalArgumentException(e);
77         }
78         return returnThis();
79     }
80 
81     /**
82      * Removes json element from the FHIR resource.
83      *
84      * @param field the element to remove.
85      * @return this builder
86      */
removeField(String field)87     public T removeField(String field) {
88         mFhir.remove(field);
89         return returnThis();
90     }
91 
92     /** Returns the current state of this builder as a JSON FHIR string. */
toJson()93     public String toJson() {
94         try {
95             return mFhir.toString(/* indentSpaces= */ 2);
96         } catch (JSONException e) {
97             // Should never happen, but JSONException is declared, and is a checked exception.
98             throw new IllegalStateException(e);
99         }
100     }
101 }
102