aboutsummaryrefslogtreecommitdiffstats
path: root/component/src/main/java/com/yahoo/component/provider/ListenableFreezableClass.java
blob: e96c362ffd8eeb7083e6eac5ac5942ca476f8302 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.component.provider;

import com.google.common.util.concurrent.ExecutionList;

import java.util.concurrent.Executor;

/**
 * A convenience superclass for listenable freezables.
 *
 * @author bratseth
 */
public class ListenableFreezableClass extends FreezableClass implements ListenableFreezable {

    private ExecutionList executionList = new ExecutionList();

    /**
     * Freezes this class to prevent further changes. Override this to freeze internal data
     * structures and dependent objects. Overrides must call super.
     * Calling freeze on an already frozen registry must have no effect.
     * <p>
     * Notifies listeners that freezing has happened.
     */
    public synchronized void freeze() {
        super.freeze();
        executionList.execute();
    }

    /** Adds a listener which will be invoked when this has become frozen. */
    @Override
    public void addFreezeListener(Runnable runnable, Executor executor) {
        executionList.add(runnable, executor);
    }

    /** Clones this. The clone is <i>not</i> frozen and has no listeners. */
    @Override
    public ListenableFreezableClass clone() {
        ListenableFreezableClass clone = (ListenableFreezableClass)super.clone();
        clone.executionList = new ExecutionList();
        return clone;
    }

}