Do not use ListenerRegistration
[mdsal.git] / binding / mdsal-binding-model-api / src / main / java / org / opendaylight / mdsal / binding / model / api / Restrictions.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. 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.binding.model.api;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.collect.ImmutableList;
12 import java.util.List;
13 import java.util.Optional;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.eclipse.jdt.annotation.Nullable;
16 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
17 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
18 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
19
20 // FIXME: MDSAL-85: specialize for supported types
21 @Beta
22 public final class Restrictions {
23     private static final @NonNull Restrictions EMPTY = new Restrictions(null, null, ImmutableList.of());
24
25     private final LengthConstraint lengthConstraint;
26     // FIXME: MDSAL-85: this is applicable only to int/uint/decimal types and it needs to be captured in the type
27     //                  itself
28     private final RangeConstraint<?> rangeConstraint;
29     private final ImmutableList<PatternConstraint> patternConstraints;
30
31     private Restrictions(final LengthConstraint lengthConstraint, final RangeConstraint<?> rangeConstraint,
32             final List<PatternConstraint> patternConstraints) {
33         this.lengthConstraint = lengthConstraint;
34         this.rangeConstraint = rangeConstraint;
35         this.patternConstraints = ImmutableList.copyOf(patternConstraints);
36     }
37
38     public static @NonNull Restrictions empty() {
39         return EMPTY;
40     }
41
42     public static @NonNull Restrictions of(final @Nullable LengthConstraint lengthConstraint) {
43         return lengthConstraint == null ? EMPTY : new Restrictions(lengthConstraint, null, ImmutableList.of());
44     }
45
46     public static @NonNull Restrictions of(final @Nullable RangeConstraint<?> rangeConstraint) {
47         return rangeConstraint == null ? EMPTY : new Restrictions(null, rangeConstraint, ImmutableList.of());
48     }
49
50     public static @NonNull Restrictions of(final List<PatternConstraint> patternConstraints,
51             final @Nullable LengthConstraint lengthConstraint) {
52         return patternConstraints.isEmpty() && lengthConstraint == null ? EMPTY
53             : new Restrictions(lengthConstraint, null, patternConstraints);
54     }
55
56     public Optional<LengthConstraint> getLengthConstraint() {
57         return Optional.ofNullable(lengthConstraint);
58     }
59
60     public List<PatternConstraint> getPatternConstraints() {
61         return patternConstraints;
62     }
63
64     public Optional<RangeConstraint<?>> getRangeConstraint() {
65         return Optional.ofNullable(rangeConstraint);
66     }
67
68     public boolean isEmpty() {
69         return lengthConstraint == null && rangeConstraint == null && patternConstraints.isEmpty();
70     }
71 }