Fix OSGiDOMSchemaService
[mdsal.git] / dom / mdsal-dom-schema-osgi / src / main / java / org / opendaylight / mdsal / dom / schema / osgi / ModelGenerationAware.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.dom.schema.osgi;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.primitives.UnsignedLong;
12 import org.eclipse.jdt.annotation.NonNull;
13 import org.opendaylight.yangtools.concepts.Immutable;
14 import org.osgi.framework.Constants;
15
16 /**
17  * Additional interface for exposing linear generation of the backing effective model. Implementations of this interface
18  * are expected to be effectively-immutable.
19  *
20  * @param <S> service type
21  */
22 @Beta
23 public interface ModelGenerationAware<S> extends Immutable {
24
25     @NonNull UnsignedLong getGeneration();
26
27     @NonNull S getService();
28
29     /**
30      * Get service ranking based on the generation. Higher generation results in a higher ranking.
31      *
32      * @return Ranging for use with {@link Constants#SERVICE_RANKING}
33      */
34     default @NonNull Integer getServiceRanking() {
35         return computeServiceRanking(getGeneration().longValue());
36     }
37
38     /**
39      * Calculate service ranking based on generation. Higher generation results in a higher ranking.
40      *
41      * @param generation generation number, treated as an unsigned long
42      * @return Ranging for use with {@link Constants#SERVICE_RANKING}
43      */
44     static @NonNull Integer computeServiceRanking(final long generation) {
45         return generation >= 0 && generation <= Integer.MAX_VALUE ? (int) generation : Integer.MAX_VALUE;
46     }
47 }