Skip to content

Method: isEmpty()

1: /*
2: * JB4JSON-LD
3: * Copyright (C) 2023 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jsonld.serialization.model;
19:
20: import java.util.Collection;
21: import java.util.Collections;
22: import java.util.Objects;
23:
24: public abstract class CompositeNode<T extends Collection<JsonNode>> extends JsonNode {
25:
26: final T items;
27: private boolean open;
28:
29: public CompositeNode() {
30: this.items = initItems();
31: this.open = true;
32: }
33:
34: public CompositeNode(String name) {
35: super(name);
36: this.items = initItems();
37: this.open = true;
38: }
39:
40: abstract T initItems();
41:
42: public void addItem(JsonNode item) {
43: Objects.requireNonNull(item);
44: items.add(item);
45: }
46:
47: public void prependItem(JsonNode item) {
48: throw new UnsupportedOperationException("Prepending items is not supported by this composite node.");
49: }
50:
51: public Collection<JsonNode> getItems() {
52: return Collections.unmodifiableCollection(items);
53: }
54:
55: public boolean isEmpty() {
56: return items.isEmpty();
57: }
58:
59: public void close() {
60: this.open = false;
61: }
62:
63: public boolean isOpen() {
64: return open;
65: }
66:
67: @Override
68: public boolean equals(Object o) {
69: if (this == o) {
70: return true;
71: }
72: if (o == null || getClass() != o.getClass()) {
73: return false;
74: }
75: CompositeNode<?> that = (CompositeNode<?>) o;
76: return open == that.open && items.equals(that.items);
77: }
78:
79: @Override
80: public int hashCode() {
81: return Objects.hash(items, open);
82: }
83: }