d478d3e951db32d9f2941bea61906dbaa501eed1
[netconf.git] / protocol / restconf-api / src / main / java / org / opendaylight / restconf / api / query / 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.api.query;
9
10 import java.net.URI;
11 import org.eclipse.jdt.annotation.NonNull;
12 import org.eclipse.jdt.annotation.Nullable;
13
14 /**
15  * This class represents a {@code depth} parameter as defined in
16  * <a href="https://www.rfc-editor.org/rfc/rfc8040#section-4.8.2">RFC8040 section 4.8.2</a>.
17  */
18 public final class DepthParam implements RestconfQueryParam<DepthParam> {
19     // API consistency: must not be confused with enum constants
20     @SuppressWarnings("checkstyle:ConstantName")
21     public static final @NonNull String uriName = "depth";
22
23     private static final @NonNull URI CAPABILITY = URI.create("urn:ietf:params:restconf:capability:depth:1.0");
24     private static final @NonNull DepthParam MIN = new DepthParam(1);
25     private static final @NonNull DepthParam MAX = new DepthParam(65535);
26
27     private final int value;
28
29     private DepthParam(final int value) {
30         this.value = value;
31     }
32
33     public static @NonNull DepthParam of(final int value) {
34         return switch (value) {
35             case 1 -> min();
36             case 65535 -> max();
37             default -> {
38                 if (value < 1 || value > 65535) {
39                     throw new IllegalArgumentException(value + " is not between 1 and 65535");
40                 }
41                 yield new DepthParam(value);
42             }
43         };
44     }
45
46     @Override
47     public Class<DepthParam> javaClass() {
48         return DepthParam.class;
49     }
50
51     @Override
52     public String paramName() {
53         return uriName;
54     }
55
56     @Override
57     public String paramValue() {
58         return String.valueOf(value);
59     }
60
61     public static @NonNull DepthParam min() {
62         return MIN;
63     }
64
65     public static @NonNull DepthParam max() {
66         return MAX;
67     }
68
69     public static @Nullable DepthParam forUriValue(final String uriValue) {
70         if ("unbounded".equals(uriValue)) {
71             return null;
72         }
73
74         final int value;
75         try {
76             value = Integer.parseUnsignedInt(uriValue, 10);
77         } catch (NumberFormatException e) {
78             throw new IllegalArgumentException(
79                 "The depth parameter must be \"unbounded\" or an integer between 1 and 65535. \""
80                     + uriValue + "\" is not a valid integer", e);
81         }
82         try {
83             return of(value);
84         } catch (IllegalArgumentException e) {
85             throw new IllegalArgumentException(
86                 "The depth parameter must be \"unbounded\" or an integer between 1 and 65535. "
87                     + e.getMessage(), e);
88         }
89     }
90
91     public int value() {
92         return value;
93     }
94
95     public static @NonNull URI capabilityUri() {
96         return CAPABILITY;
97     }
98 }