1 /*
2  * Copyright (C) 2020 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 com.android.server.utils;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 
22 import com.android.internal.annotations.GuardedBy;
23 
24 import java.util.ArrayList;
25 import java.util.Objects;
26 
27 /**
28  * A concrete implementation of {@link Watchable}.  This includes one commonly needed feature:
29  * the Watchable may be sealed, so that it throws an {@link IllegalStateException} if
30  * a change is detected.
31  */
32 public class WatchableImpl implements Watchable {
33     /**
34      * The list of observers.
35      */
36     protected final ArrayList<Watcher> mObservers = new ArrayList<>();
37 
38     /**
39      * Ensure the observer is the list. The observer cannot be null but it is okay if it
40      * is already in the list.
41      *
42      * @param observer The {@link} Watcher to be added to the notification list.
43      */
44     @Override
registerObserver(@onNull Watcher observer)45     public void registerObserver(@NonNull Watcher observer) {
46         Objects.requireNonNull(observer, "observer may not be null");
47         synchronized (mObservers) {
48             if (!mObservers.contains(observer)) {
49                 mObservers.add(observer);
50             }
51         }
52     }
53 
54     /**
55      * Removes a previously registered observer. The observer must not be null and it
56      * must already have been registered.
57      *
58      * @param observer The {@link} Watcher to be removed from the notification list.
59      */
60     @Override
unregisterObserver(@onNull Watcher observer)61     public void unregisterObserver(@NonNull Watcher observer) {
62         Objects.requireNonNull(observer, "observer may not be null");
63         synchronized (mObservers) {
64             mObservers.remove(observer);
65         }
66     }
67 
68     /**
69      * Return true if the {@link Watcher) is a registered observer.
70      * @param observer A {@link Watcher} that might be registered
71      * @return true if the observer is registered with this {@link Watchable}.
72      */
73     @Override
isRegisteredObserver(@onNull Watcher observer)74     public boolean isRegisteredObserver(@NonNull Watcher observer) {
75         synchronized (mObservers) {
76             return mObservers.contains(observer);
77         }
78     }
79 
80     /**
81      * Return the number of registered observers.
82      *
83      * @return The number of registered observers.
84      */
registeredObserverCount()85     public int registeredObserverCount() {
86         return mObservers.size();
87     }
88 
89     /**
90      * Invokes {@link Watcher#onChange} on each observer.
91      *
92      * @param what The {@link Watchable} that generated the event
93      */
94     @Override
dispatchChange(@ullable Watchable what)95     public void dispatchChange(@Nullable Watchable what) {
96         synchronized (mObservers) {
97             if (mSealed) {
98                 throw new IllegalStateException("attempt to change a sealed object");
99             }
100             final int end = mObservers.size();
101             for (int i = 0; i < end; i++) {
102                 mObservers.get(i).onChange(what);
103             }
104         }
105     }
106 
107     /**
108      * True if the object is sealed.
109      */
110     @GuardedBy("mObservers")
111     private boolean mSealed = false;
112 
113     /**
114      * Freeze the {@link Watchable}.
115      */
seal()116     public void seal() {
117         synchronized (mObservers) {
118             mSealed = true;
119         }
120     }
121 
122     /**
123      * Return the sealed state.
124      */
isSealed()125     public boolean isSealed() {
126         synchronized (mObservers) {
127             return mSealed;
128         }
129     }
130 }
131