2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.protocol.bgp.parser.spi.pojo;
10 import com.google.common.base.Preconditions;
13 import java.util.concurrent.ConcurrentHashMap;
15 import org.opendaylight.protocol.concepts.AbstractRegistration;
17 abstract class AbstractFamilyRegistry<C, N> {
18 private final Map<Class<? extends C>, N> classToNumber = new ConcurrentHashMap<>();
19 private final Map<N, Class<? extends C>> numberToClass = new ConcurrentHashMap<>();
21 protected synchronized AutoCloseable registerFamily(final Class<? extends C> clazz, final N number) {
22 Preconditions.checkNotNull(clazz);
24 final Class<?> c = this.numberToClass.get(number);
25 Preconditions.checkState(c == null, "Number " + number + " already registered to " + c);
27 final N n = this.classToNumber.get(clazz);
28 Preconditions.checkState(n == null, "Class " + clazz + " already registered to " + n);
30 this.numberToClass.put(number, clazz);
31 this.classToNumber.put(clazz, number);
33 final Object lock = this;
34 return new AbstractRegistration() {
37 protected void removeRegistration() {
39 AbstractFamilyRegistry.this.classToNumber.remove(clazz);
40 AbstractFamilyRegistry.this.numberToClass.remove(number);
46 protected Class<? extends C> classForFamily(final N number) {
47 return this.numberToClass.get(number);
50 protected N numberForClass(final Class<? extends C> clazz) {
51 return this.classToNumber.get(clazz);