Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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.incubator.net.l2monitoring.cfm; |
| 17 | |
| 18 | import java.util.ArrayList; |
| 19 | import java.util.Collection; |
| 20 | |
| 21 | import com.google.common.collect.Lists; |
| 22 | import org.onlab.packet.VlanId; |
| 23 | |
| 24 | /** |
| 25 | * The default implementation of {@link org.onosproject.incubator.net.l2monitoring.cfm.Component}. |
| 26 | */ |
| 27 | public final class DefaultComponent implements Component { |
| 28 | |
| 29 | private final int componentId; |
| 30 | private final Collection<VlanId> vidList; |
| 31 | private final MhfCreationType mhfCreationType; |
| 32 | private final IdPermissionType idPermission; |
| 33 | private final TagType tagType; |
| 34 | |
| 35 | private DefaultComponent(DefaultComponentBuilder builder) { |
| 36 | this.componentId = builder.componentId; |
| 37 | this.vidList = builder.vidList; |
| 38 | this.mhfCreationType = builder.mhfCreationType; |
| 39 | this.idPermission = builder.idPermission; |
| 40 | this.tagType = builder.tagType; |
| 41 | } |
| 42 | |
| 43 | @Override |
| 44 | public int componentId() { |
| 45 | return componentId; |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public Collection<VlanId> vidList() { |
| 50 | if (vidList != null) { |
| 51 | return Lists.newArrayList(vidList); |
| 52 | } else { |
| 53 | return null; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public MhfCreationType mhfCreationType() { |
| 59 | return mhfCreationType; |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public IdPermissionType idPermission() { |
| 64 | return idPermission; |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public TagType tagType() { |
| 69 | return tagType; |
| 70 | } |
| 71 | |
| 72 | |
| 73 | |
| 74 | @Override |
| 75 | public int hashCode() { |
| 76 | final int prime = 31; |
| 77 | int result = 1; |
| 78 | result = prime * result + componentId; |
| 79 | result = prime * result |
| 80 | + ((idPermission == null) ? 0 : idPermission.hashCode()); |
| 81 | result = prime * result |
| 82 | + ((mhfCreationType == null) ? 0 : mhfCreationType.hashCode()); |
| 83 | result = prime * result + ((tagType == null) ? 0 : tagType.hashCode()); |
| 84 | result = prime * result + ((vidList == null) ? 0 : vidList.hashCode()); |
| 85 | return result; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public boolean equals(Object obj) { |
| 90 | if (this == obj) { |
| 91 | return true; |
| 92 | } |
| 93 | if (obj == null) { |
| 94 | return false; |
| 95 | } |
| 96 | if (getClass() != obj.getClass()) { |
| 97 | return false; |
| 98 | } |
| 99 | DefaultComponent other = (DefaultComponent) obj; |
| 100 | if (componentId != other.componentId) { |
| 101 | return false; |
| 102 | } |
| 103 | if (idPermission != other.idPermission) { |
| 104 | return false; |
| 105 | } |
| 106 | if (mhfCreationType != other.mhfCreationType) { |
| 107 | return false; |
| 108 | } |
| 109 | if (tagType != other.tagType) { |
| 110 | return false; |
| 111 | } |
| 112 | if (vidList == null) { |
| 113 | if (other.vidList != null) { |
| 114 | return false; |
| 115 | } |
| 116 | } else if (!vidList.equals(other.vidList)) { |
| 117 | return false; |
| 118 | } |
| 119 | return true; |
| 120 | } |
| 121 | |
| 122 | public static ComponentBuilder builder(int componentId) { |
| 123 | return new DefaultComponentBuilder(componentId); |
| 124 | } |
| 125 | |
| 126 | private static final class DefaultComponentBuilder implements ComponentBuilder { |
| 127 | private final int componentId; |
| 128 | private Collection<VlanId> vidList; |
| 129 | private MhfCreationType mhfCreationType; |
| 130 | private IdPermissionType idPermission; |
| 131 | private TagType tagType; |
| 132 | |
| 133 | private DefaultComponentBuilder(int componentId) { |
| 134 | this.componentId = componentId; |
| 135 | vidList = new ArrayList<>(); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public ComponentBuilder addToVidList(VlanId vid) { |
| 140 | this.vidList.add(vid); |
| 141 | return this; |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public ComponentBuilder mhfCreationType(MhfCreationType mhfCreationType) { |
| 146 | this.mhfCreationType = mhfCreationType; |
| 147 | return this; |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public ComponentBuilder idPermission(IdPermissionType idPermission) { |
| 152 | this.idPermission = idPermission; |
| 153 | return this; |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public ComponentBuilder tagType(TagType tagType) { |
| 158 | this.tagType = tagType; |
| 159 | return this; |
| 160 | } |
| 161 | |
| 162 | public Component build() { |
| 163 | return new DefaultComponent(this); |
| 164 | } |
| 165 | } |
| 166 | } |