blob: de3a2d20751ddeb3d63cc8cfdfcf1d4be0ecc66b [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2015 The Bazel Authors. All rights reserved.
Janak Ramakrishnan551bb092015-06-15 21:09:25 +00002//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// 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
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14package com.google.devtools.build.lib.util;
15
Janak Ramakrishnan551bb092015-06-15 21:09:25 +000016import java.util.regex.Pattern;
17
18/**
19 * Shim to work around the issue that {@link Pattern} doesn't implement equality. Treats pattern as
20 * a {@link String} for equality-checking purposes. Thus, if two PatternWithEquality objects are
21 * equal, their internal Pattern objects are necessarily equal, although the converse does not hold.
22 */
23public final class PatternWithEquality {
24 public final Pattern pattern;
25
26 public PatternWithEquality(Pattern pattern) {
27 this.pattern = Preconditions.checkNotNull(pattern);
28 }
29
30 @Override
31 public int hashCode() {
32 return pattern.toString().hashCode();
33 }
34
35 @Override
36 public boolean equals(Object other) {
37 return (other instanceof PatternWithEquality)
38 && pattern.toString().equals(((PatternWithEquality) other).pattern.toString());
39 }
40}