Home | History | Annotate | Download | only in collect
      1 /*
      2  * Copyright (C) 2011 The Guava Authors
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
      5  * in compliance with the License. You may obtain a copy of the License at
      6  *
      7  * http://www.apache.org/licenses/LICENSE-2.0
      8  *
      9  * Unless required by applicable law or agreed to in writing, software distributed under the
     10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
     11  * express or implied. See the License for the specific language governing permissions and
     12  * limitations under the License.
     13  */
     14 
     15 package com.google.common.collect;
     16 
     17 import static com.google.common.base.Preconditions.checkNotNull;
     18 
     19 import com.google.common.annotations.GwtCompatible;
     20 
     21 /**
     22  * A rule for a local mutation to a binary search tree, that changes at most one entry. In addition
     23  * to specifying how it modifies a particular entry via a {@code BstModifier}, it specifies a
     24  * {@link BstBalancePolicy} for rebalancing the tree after the modification is performed and a
     25  * {@link BstNodeFactory} for constructing newly rebalanced nodes.
     26  *
     27  * @author Louis Wasserman
     28  * @param <K> The key type of the nodes in binary search trees that this rule can modify.
     29  * @param <N> The type of the nodes in binary search trees that this rule can modify.
     30  */
     31 @GwtCompatible
     32 final class BstMutationRule<K, N extends BstNode<K, N>> {
     33   /**
     34    * Constructs a {@code BstMutationRule} with the specified modifier, balance policy, and node
     35    * factory.
     36    */
     37   public static <K, N extends BstNode<K, N>> BstMutationRule<K, N> createRule(
     38       BstModifier<K, N> modifier, BstBalancePolicy<N> balancePolicy,
     39       BstNodeFactory<N> nodeFactory) {
     40     return new BstMutationRule<K, N>(modifier, balancePolicy, nodeFactory);
     41   }
     42 
     43   private final BstModifier<K, N> modifier;
     44   private final BstBalancePolicy<N> balancePolicy;
     45   private final BstNodeFactory<N> nodeFactory;
     46 
     47   private BstMutationRule(BstModifier<K, N> modifier, BstBalancePolicy<N> balancePolicy,
     48       BstNodeFactory<N> nodeFactory) {
     49     this.balancePolicy = checkNotNull(balancePolicy);
     50     this.nodeFactory = checkNotNull(nodeFactory);
     51     this.modifier = checkNotNull(modifier);
     52   }
     53 
     54   /**
     55    * Returns the {@link BstModifier} that specifies the change to a targeted entry in a binary
     56    * search tree.
     57    */
     58   public BstModifier<K, N> getModifier() {
     59     return modifier;
     60   }
     61 
     62   /**
     63    * Returns the policy used to rebalance nodes in the tree after this modification has been
     64    * performed.
     65    */
     66   public BstBalancePolicy<N> getBalancePolicy() {
     67     return balancePolicy;
     68   }
     69 
     70   /**
     71    * Returns the node factory used to create new nodes in the tree after this modification has been
     72    * performed.
     73    */
     74   public BstNodeFactory<N> getNodeFactory() {
     75     return nodeFactory;
     76   }
     77 }
     78