aboutsummaryrefslogtreecommitdiffstats
path: root/component/src/main/java/com/yahoo/component/provider/FreezableClass.java
blob: ac8888aeaec2df06dcd9678eefd3e7e2ff1b616e (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
44
45
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.component.provider;

/**
 * Convenience superclass of non-component freezables
 *
 * @author bratseth
 */
public class FreezableClass implements Freezable {

    /** True when this cannot be changed any more */
    private boolean frozen=false;

    /**
     * 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.
     */
    public synchronized void freeze() {
        frozen=true;
    }

    /** Returns whether this is currently frozen */
    public final boolean isFrozen() { return frozen; }

    /** Throws an IllegalStateException if this is frozen */
    protected void ensureNotFrozen() {
        if (frozen)
            throw new IllegalStateException(this + " is frozen and cannot be modified");
    }

    /** Clones this. The clone is <i>not</i> frozen */
    @Override
    public FreezableClass clone() {
        try {
            FreezableClass clone=(FreezableClass)super.clone();
            clone.frozen = false;
            return clone;
        }
        catch (CloneNotSupportedException e) {
            throw new RuntimeException(e);
        }
    }

}