Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2015 The Bazel Authors. All rights reserved. |
Janak Ramakrishnan | 551bb09 | 2015-06-15 21:09:25 +0000 | [diff] [blame] | 2 | // |
| 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. |
| 14 | package com.google.devtools.build.lib.util; |
| 15 | |
Janak Ramakrishnan | 551bb09 | 2015-06-15 21:09:25 +0000 | [diff] [blame] | 16 | import 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 | */ |
| 23 | public 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 | } |