Jian Li | a0bf459 | 2017-02-03 17:43:21 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Laboratory |
| 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 | package org.onosproject.mapping; |
| 17 | |
Jian Li | 44155b0 | 2017-02-15 17:03:38 +0900 | [diff] [blame] | 18 | import com.google.common.base.MoreObjects; |
Jian Li | 0e09eaa | 2017-02-14 02:01:18 +0900 | [diff] [blame] | 19 | import org.onosproject.mapping.addresses.MappingAddress; |
Jian Li | 136fd6c | 2017-02-10 14:54:59 +0900 | [diff] [blame] | 20 | |
Jian Li | 44155b0 | 2017-02-15 17:03:38 +0900 | [diff] [blame] | 21 | import java.util.Objects; |
| 22 | |
Jian Li | a0bf459 | 2017-02-03 17:43:21 +0900 | [diff] [blame] | 23 | /** |
| 24 | * Default mapping key implementation. |
| 25 | */ |
Jian Li | 44155b0 | 2017-02-15 17:03:38 +0900 | [diff] [blame] | 26 | public final class DefaultMappingKey implements MappingKey { |
| 27 | |
| 28 | private final MappingAddress address; |
| 29 | |
| 30 | /** |
| 31 | * Create a new mapping key from the specified mapping address. |
| 32 | * |
| 33 | * @param address a mapping address |
| 34 | */ |
| 35 | private DefaultMappingKey(MappingAddress address) { |
| 36 | this.address = address; |
| 37 | } |
| 38 | |
Jian Li | 136fd6c | 2017-02-10 14:54:59 +0900 | [diff] [blame] | 39 | @Override |
| 40 | public MappingAddress address() { |
Jian Li | 44155b0 | 2017-02-15 17:03:38 +0900 | [diff] [blame] | 41 | return address; |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | public int hashCode() { |
| 46 | return Objects.hash(address); |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public boolean equals(Object obj) { |
| 51 | if (this == obj) { |
| 52 | return true; |
| 53 | } |
| 54 | if (obj instanceof DefaultMappingKey) { |
| 55 | DefaultMappingKey that = (DefaultMappingKey) obj; |
| 56 | return Objects.equals(address, that.address); |
| 57 | } |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public String toString() { |
| 63 | return MoreObjects.toStringHelper(getClass()) |
| 64 | .add("address", address) |
| 65 | .toString(); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Returns a new mapping key builder. |
| 70 | * |
| 71 | * @return mapping key builder |
| 72 | */ |
| 73 | public static MappingKey.Builder builder() { |
| 74 | return new Builder(); |
| 75 | } |
| 76 | |
Jian Li | cc169f3 | 2017-02-24 20:13:23 +0900 | [diff] [blame] | 77 | /** |
| 78 | * Returns a new mapping key builder. |
| 79 | * |
| 80 | * @param key mapping key |
| 81 | * @return mapping key builder |
| 82 | */ |
Jian Li | 44155b0 | 2017-02-15 17:03:38 +0900 | [diff] [blame] | 83 | public static MappingKey.Builder builder(MappingKey key) { |
| 84 | return new Builder(key); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Builds a mapping key. |
| 89 | */ |
| 90 | public static final class Builder implements MappingKey.Builder { |
| 91 | |
| 92 | private MappingAddress address; |
| 93 | |
| 94 | // creates a new builder |
| 95 | private Builder() { |
| 96 | } |
| 97 | |
| 98 | // creates a new builder based off an existing mapping key |
| 99 | private Builder(MappingKey key) { |
| 100 | this.address = key.address(); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public MappingKey.Builder withAddress(MappingAddress address) { |
| 105 | this.address = address; |
| 106 | return this; |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public MappingKey build() { |
| 111 | |
| 112 | return new DefaultMappingKey(address); |
| 113 | } |
Jian Li | 136fd6c | 2017-02-10 14:54:59 +0900 | [diff] [blame] | 114 | } |
Jian Li | a0bf459 | 2017-02-03 17:43:21 +0900 | [diff] [blame] | 115 | } |