2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Pantheon.tech
4 * Copyright (C) 2022 Nordix Foundation
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.spi;
23 import lombok.RequiredArgsConstructor;
25 @RequiredArgsConstructor
26 public class FetchDescendantsOption {
28 public static final FetchDescendantsOption FETCH_DIRECT_CHILDREN_ONLY = new FetchDescendantsOption(1);
29 public static final FetchDescendantsOption OMIT_DESCENDANTS = new FetchDescendantsOption(0);
30 public static final FetchDescendantsOption INCLUDE_ALL_DESCENDANTS = new FetchDescendantsOption(-1);
32 private final int depth;
37 * @return true if next level of depth is available
38 * @throws IllegalArgumentException when depth less than -1
40 public boolean hasNext() {
42 return depth > 0 || this.depth == INCLUDE_ALL_DESCENDANTS.depth;
46 * Next fetch descendants option.
48 * @return the next fetch descendants option
49 * @throws IllegalArgumentException when depth less than -1 or 0
51 public FetchDescendantsOption next() {
53 throw new IllegalArgumentException("Do not use next() method with zero depth");
55 final FetchDescendantsOption nextDescendantsOption = this.depth == INCLUDE_ALL_DESCENDANTS.depth
56 ? INCLUDE_ALL_DESCENDANTS : new FetchDescendantsOption(depth - 1);
57 validateDepth(nextDescendantsOption.depth);
58 return nextDescendantsOption;
61 private static void validateDepth(final int depth) {
63 throw new IllegalArgumentException("A depth of less than minus one is not allowed");