Reduce exception guard
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / DepthParam.java
1 /*
2  * Copyright (c) 2021 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.restconf.nb.rfc8040;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
12 import com.google.common.annotations.Beta;
13 import java.net.URI;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.eclipse.jdt.annotation.Nullable;
16
17 /**
18  * This class represents a {@code depth} parameter as defined in
19  * <a href="https://datatracker.ietf.org/doc/html/rfc8040#section-4.8.2">RFC8040 section 4.8.2</a>.
20  */
21 public final class DepthParam implements RestconfQueryParam<DepthParam> {
22     // API consistency: must not be confused with enum constants
23     @SuppressWarnings("checkstyle:ConstantName")
24     public static final @NonNull String uriName = "depth";
25
26     private static final @NonNull URI CAPABILITY = URI.create("urn:ietf:params:restconf:capability:depth:1.0");
27     private static final @NonNull DepthParam MIN = of(1);
28     private static final @NonNull DepthParam MAX = of(65535);
29
30     private final int value;
31
32     private DepthParam(final int value) {
33         this.value = value;
34         checkArgument(value >= 1 && value <= 65535);
35     }
36
37     public static @NonNull DepthParam of(final int value) {
38         return new DepthParam(value);
39     }
40
41     @Override
42     public Class<@NonNull DepthParam> javaClass() {
43         return DepthParam.class;
44     }
45
46     @Override
47     public String paramName() {
48         return uriName;
49     }
50
51     @Override
52     public String paramValue() {
53         return String.valueOf(value);
54     }
55
56     @Beta
57     public static @NonNull DepthParam min() {
58         return MIN;
59     }
60
61     @Beta
62     public static @NonNull DepthParam max() {
63         return MAX;
64     }
65
66     public static @Nullable DepthParam forUriValue(final String uriValue) {
67         return "unbounded".equals(uriValue) ? null : of(Integer.parseUnsignedInt(uriValue, 10));
68     }
69
70     public int value() {
71         return value;
72     }
73
74     public static @NonNull URI capabilityUri() {
75         return CAPABILITY;
76     }
77 }