Home | History | Annotate | Download | only in provider
      1 /*
      2  * Copyright (C) 2017 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 
     18 package android.provider;
     19 
     20 /**
     21  * A builder that facilitates prohibiting its use after an instance was created with it.
     22  *
     23  * Suggested usage:
     24  * call {@link #checkNotUsed} in each setter, and {@link #markUsed} in {@link #build}
     25  *
     26  * @param <T> Type of object being built
     27  * @hide
     28  */
     29 public abstract class OneTimeUseBuilder<T> {
     30     private boolean used = false;
     31 
     32     protected void markUsed() {
     33         checkNotUsed();
     34         used = true;
     35     }
     36 
     37     protected void checkNotUsed() {
     38         if (used) {
     39             throw new IllegalStateException(
     40                     "This Builder should not be reused. Use a new Builder instance instead");
     41         }
     42     }
     43 
     44     /**
     45      * Builds the instance
     46      *
     47      * Once this method is called, this builder should no longer be used. Any subsequent calls to a
     48      * setter or {@code build()} will throw an exception
     49      */
     50     public abstract T build();
     51 }
     52