blob: 1fdad9e60ffeee6c916759927e063ef622682980 [file] [log] [blame]
Jian Lia0bf4592017-02-03 17:43:21 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jian Lia0bf4592017-02-03 17:43:21 +09003 *
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 */
16package org.onosproject.mapping;
17
Jian Li44155b02017-02-15 17:03:38 +090018import com.google.common.base.MoreObjects;
Jian Li0e09eaa2017-02-14 02:01:18 +090019import org.onosproject.mapping.addresses.MappingAddress;
Jian Li136fd6c2017-02-10 14:54:59 +090020
Jian Li44155b02017-02-15 17:03:38 +090021import java.util.Objects;
22
Jian Lia0bf4592017-02-03 17:43:21 +090023/**
24 * Default mapping key implementation.
25 */
Jian Li44155b02017-02-15 17:03:38 +090026public 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 Li136fd6c2017-02-10 14:54:59 +090039 @Override
40 public MappingAddress address() {
Jian Li44155b02017-02-15 17:03:38 +090041 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 Licc169f32017-02-24 20:13:23 +090077 /**
78 * Returns a new mapping key builder.
79 *
80 * @param key mapping key
81 * @return mapping key builder
82 */
Jian Li44155b02017-02-15 17:03:38 +090083 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 Li136fd6c2017-02-10 14:54:59 +0900114 }
Jian Lia0bf4592017-02-03 17:43:21 +0900115}