aboutsummaryrefslogtreecommitdiffstats
path: root/component/src/main/java/com/yahoo/component/provider/FreezableSimpleComponent.java
blob: 1719cf96a198acec02bc6f4498e194fd1b60f3bb (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
46
47
// 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.yahoo.component.ComponentId;
import com.yahoo.component.AbstractComponent;

/**
 * Superclass for simple freezable components
 *
 * @author bratseth
 */
public class FreezableSimpleComponent extends AbstractComponent implements Freezable {

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

    protected FreezableSimpleComponent(ComponentId id) {
        super(id);
    }

    protected FreezableSimpleComponent() {}

    /**
     * Freezes this component 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 will <i>not</i> be frozen */
    @Override
    public FreezableSimpleComponent clone() {
        FreezableSimpleComponent clone=(FreezableSimpleComponent)super.clone();
        clone.frozen = false;
        return clone;
    }

}