• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2014 Google Inc. All rights reserved.
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 License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 
15 package com.google.googlejavaformat.java;
16 
17 import static com.google.common.base.Preconditions.checkArgument;
18 import static com.google.common.base.Preconditions.checkNotNull;
19 
20 import com.google.common.collect.Range;
21 import java.util.Objects;
22 
23 /**
24  * Represents a range in the original source and replacement text for that range.
25  *
26  * <p>google-java-format doesn't depend on AutoValue, to allow AutoValue to depend on
27  * google-java-format.
28  */
29 public final class Replacement {
30 
create(int startPosition, int endPosition, String replaceWith)31   public static Replacement create(int startPosition, int endPosition, String replaceWith) {
32     checkArgument(startPosition >= 0, "startPosition must be non-negative");
33     checkArgument(startPosition <= endPosition, "startPosition cannot be after endPosition");
34     return new Replacement(Range.closedOpen(startPosition, endPosition), replaceWith);
35   }
36 
37   private final Range<Integer> replaceRange;
38   private final String replacementString;
39 
Replacement(Range<Integer> replaceRange, String replacementString)40   private Replacement(Range<Integer> replaceRange, String replacementString) {
41     this.replaceRange = checkNotNull(replaceRange, "Null replaceRange");
42     this.replacementString = checkNotNull(replacementString, "Null replacementString");
43   }
44 
45   /** The range of characters in the original source to replace. */
getReplaceRange()46   public Range<Integer> getReplaceRange() {
47     return replaceRange;
48   }
49 
50   /** The string to replace the range of characters with. */
getReplacementString()51   public String getReplacementString() {
52     return replacementString;
53   }
54 
55   @Override
equals(Object o)56   public boolean equals(Object o) {
57     if (o == this) {
58       return true;
59     }
60     if (o instanceof Replacement) {
61       Replacement that = (Replacement) o;
62       return replaceRange.equals(that.getReplaceRange())
63           && replacementString.equals(that.getReplacementString());
64     }
65     return false;
66   }
67 
68   @Override
hashCode()69   public int hashCode() {
70     return Objects.hash(replaceRange, replacementString);
71   }
72 }
73