File: OneOrListOf.java

package info (click to toggle)
python-schema-salad 8.9.20251102115403-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,060 kB
  • sloc: python: 19,247; cpp: 2,631; cs: 1,869; java: 1,341; makefile: 187; xml: 184; sh: 103; javascript: 46
file content (48 lines) | stat: -rw-r--r-- 1,113 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package ${package}.utils;

import java.util.List;
import java.util.Optional;

public class OneOrListOf<T> {
    private Optional<T> object;
    private Optional<List<T>> objects;

    private OneOrListOf(final T object, final List<T> objects) {
        this.object = Optional.ofNullable(object);
        this.objects = Optional.ofNullable(objects);
    }

    public static <T> OneOrListOf<T> oneOf(T object) {
        return new OneOrListOf(object, null);
    }

    public static <T> OneOrListOf<T> listOf(List<T> objects) {
        assert objects != null;
        return new OneOrListOf(null, objects);
    }

    public boolean isOne() {
        return this.getOneOptional().isPresent();
    }

    public boolean isList() {
        return this.getListOptional().isPresent();
    }

    public Optional<T> getOneOptional() {
        return this.object;
    }
    
    public Optional<List<T>> getListOptional() {
        return this.objects;
    }

    public T getOne() {
        return this.getOneOptional().get();
    }

    public List<T> getList() {
        return this.getListOptional().get();
    }

}