blob: d8288801f9fb52190910d86f38e2af8ca9962afa [file] [log] [blame]
Thomas Vachuska48448082016-02-19 22:14:54 -08001/*
2 * Copyright 2016 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
17package org.onosproject.net.region;
18
19import com.google.common.base.MoreObjects;
20import com.google.common.collect.ImmutableList;
21import org.onosproject.cluster.NodeId;
22
23import java.util.List;
24import java.util.Objects;
25import java.util.Set;
26
27/**
28 * Default implementation of a region.
29 */
30public final class DefaultRegion implements Region {
31
32 private final RegionId id;
33 private final String name;
34 private final Type type;
35 private final List<Set<NodeId>> masters;
36
37 /**
38 * Creates a region using the supplied information.
39 *
40 * @param id region identifier
41 * @param name friendly name
42 * @param type region type
43 * @param masters list of sets of cluster node identifiers; in order of mastership
44 */
45 public DefaultRegion(RegionId id, String name, Type type, List<Set<NodeId>> masters) {
46 this.id = id;
47 this.name = name;
48 this.type = type;
49 this.masters = masters != null ? ImmutableList.copyOf(masters) : ImmutableList.of();
50 }
51
52 @Override
53 public RegionId id() {
54 return id;
55 }
56
57 @Override
58 public String name() {
59 return name;
60 }
61
62 @Override
63 public Type type() {
64 return type;
65 }
66
67 @Override
68 public List<Set<NodeId>> masters() {
69 return masters;
70 }
71
72 @Override
73 public int hashCode() {
74 return Objects.hash(id, name, type, masters);
75 }
76
77 @Override
78 public boolean equals(Object obj) {
79 if (this == obj) {
80 return true;
81 }
82 if (obj instanceof DefaultRegion) {
83 final DefaultRegion that = (DefaultRegion) obj;
84 return Objects.equals(this.id, that.id)
85 && Objects.equals(this.name, that.name)
86 && Objects.equals(this.type, that.type)
87 && Objects.equals(this.masters, that.masters);
88 }
89 return false;
90 }
91
92 @Override
93 public String toString() {
94 return MoreObjects.toStringHelper(this)
95 .add("id", id)
96 .add("name", name)
97 .add("type", type)
98 .add("masters", masters)
99 .toString();
100 }
101
102}