1 /* 2 * Copyright (C) 2007 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.dx.util; 18 19 /** 20 * Very simple base class that implements a flag to control the mutability 21 * of instances. This class just provides the flag and a utility to check 22 * and throw the right exception, but it is up to subclasses to place calls 23 * to the checker in all the right places. 24 */ 25 public class MutabilityControl { 26 /** whether this instance is mutable */ 27 private boolean mutable; 28 29 /** 30 * Constructs an instance. It is initially mutable. 31 */ MutabilityControl()32 public MutabilityControl() { 33 mutable = true; 34 } 35 36 /** 37 * Constructs an instance, explicitly indicating the mutability. 38 * 39 * @param mutable {@code true} iff this instance is mutable 40 */ MutabilityControl(boolean mutable)41 public MutabilityControl(boolean mutable) { 42 this.mutable = mutable; 43 } 44 45 /** 46 * Makes this instance immutable. 47 */ setImmutable()48 public void setImmutable() { 49 mutable = false; 50 } 51 52 /** 53 * Checks to see whether or not this instance is immutable. This is the 54 * same as calling {@code !isMutable()}. 55 * 56 * @return {@code true} iff this instance is immutable 57 */ isImmutable()58 public final boolean isImmutable() { 59 return !mutable; 60 } 61 62 /** 63 * Checks to see whether or not this instance is mutable. 64 * 65 * @return {@code true} iff this instance is mutable 66 */ isMutable()67 public final boolean isMutable() { 68 return mutable; 69 } 70 71 /** 72 * Throws {@link MutabilityException} if this instance is 73 * immutable. 74 */ throwIfImmutable()75 public final void throwIfImmutable() { 76 if (!mutable) { 77 throw new MutabilityException("immutable instance"); 78 } 79 } 80 81 /** 82 * Throws {@link MutabilityException} if this instance is mutable. 83 */ throwIfMutable()84 public final void throwIfMutable() { 85 if (mutable) { 86 throw new MutabilityException("mutable instance"); 87 } 88 } 89 } 90