• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wurstscript / WurstScript / 240

14 Jan 2024 02:16PM CUT coverage: 62.353% (-0.04%) from 62.397%
240

Pull #1087

circleci

Frotty
fix lua map config and number issues
Pull Request #1087: Fix lua map config and number issues

17273 of 27702 relevant lines covered (62.35%)

0.62 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
de.peeeq.wurstscript/src/main/java/de/peeeq/datastructures/ImmutableTree.java
1
package de.peeeq.datastructures;
2

3
import com.google.common.base.Preconditions;
4
import com.google.common.collect.ImmutableList;
5
import com.google.common.collect.ImmutableList.Builder;
6
import org.eclipse.jdt.annotation.Nullable;
7

8
import java.util.Collections;
9
import java.util.Iterator;
10

11
public class ImmutableTree<T> implements Iterable<ImmutableTree<T>> {
12

13
    // invariant: children == null <--> elem != null
14
    private final @Nullable ImmutableList<ImmutableTree<T>> children;
15
    private final @Nullable T elem;
16

17
    private ImmutableTree(@Nullable ImmutableList<ImmutableTree<T>> children, @Nullable T elem) {
×
18
        Preconditions.checkArgument((children == null) != (elem == null));
×
19
        this.children = children;
×
20
        this.elem = elem;
×
21
    }
×
22

23
    public static <T> ImmutableTree<T> node(ImmutableList<ImmutableTree<T>> children) {
24
        return new ImmutableTree<>(children, null);
×
25
    }
26

27
    public static <T> ImmutableTree<T> leaf(T t) {
28
        return new ImmutableTree<>(null, t);
×
29
    }
30

31
    public int size() {
32
        ImmutableList<ImmutableTree<T>> ch = children;
×
33
        if (ch == null) {
×
34
            return 1;
×
35
        } else {
36
            int size = 0;
×
37
            for (ImmutableTree<T> c : ch) {
×
38
                size += c.size();
×
39
            }
×
40
            return size;
×
41
        }
42
    }
43

44

45
    @Override
46
    public Iterator<ImmutableTree<T>> iterator() {
47
        final ImmutableList<ImmutableTree<T>> children2 = children;
×
48
        if (children2 != null) {
×
49
            return children2.iterator();
×
50
        } else {
51
            return Collections.emptyIterator();
×
52
        }
53
    }
54

55
    public ImmutableList<T> allValues() {
56
        Builder<T> b = ImmutableList.builder();
×
57
        addValues(b);
×
58
        return b.build();
×
59
    }
60

61
    private void addValues(Builder<@Nullable T> b) {
62
        ImmutableList<ImmutableTree<T>> ch = children;
×
63
        if (ch == null) {
×
64
            b.add(elem);
×
65
        } else {
66
            for (ImmutableTree<T> c : ch) {
×
67
                c.addValues(b);
×
68
            }
×
69
        }
70
    }
×
71

72
    public static <T> ImmutableTree<T> empty() {
73
        return node(ImmutableList.of());
×
74
    }
75

76
    @Override
77
    public int hashCode() {
78
        final int prime = 31;
×
79
        int result = 1;
×
80
        ImmutableList<ImmutableTree<T>> c = children;
×
81
        @Nullable T e = elem;
×
82
        result = prime * result + ((c == null) ? 0 : c.hashCode());
×
83
        result = prime * result + ((e == null) ? 0 : e.hashCode());
×
84
        return result;
×
85
    }
86

87
    @Override
88
    public boolean equals(@Nullable Object obj) {
89
        if (this == obj)
×
90
            return true;
×
91
        if (obj == null)
×
92
            return false;
×
93
        if (getClass() != obj.getClass())
×
94
            return false;
×
95
        ImmutableTree<?> other = (ImmutableTree<?>) obj;
×
96
        ImmutableList<ImmutableTree<T>> c = children;
×
97
        if (c == null) {
×
98
            if (other.children != null)
×
99
                return false;
×
100
        } else if (!c.equals(other.children))
×
101
            return false;
×
102
        @Nullable T e = elem;
×
103
        if (e == null) {
×
104
            return other.elem == null;
×
105
        } else return e.equals(other.elem);
×
106
    }
107

108
    @Override
109
    public String toString() {
110
        StringBuilder sb = new StringBuilder();
×
111
        ImmutableList<ImmutableTree<T>> c = children;
×
112
        if (c == null) {
×
113
            sb.append("[");
×
114
            sb.append(elem);
×
115
            sb.append("]");
×
116
        } else {
117
            sb.append("[");
×
118
            boolean first = true;
×
119
            for (ImmutableTree<T> t : c) {
×
120
                if (!first) {
×
121
                    sb.append(", ");
×
122
                }
123
                sb.append(t);
×
124
                first = false;
×
125
            }
×
126
            sb.append("]");
×
127
        }
128
        return sb.toString();
×
129
    }
130

131
    public T getOnlyEment() {
132
        ImmutableList<ImmutableTree<T>> ch = children;
×
133
        @Nullable T el = elem;
×
134
        if (el != null) {
×
135
            return el;
×
136
        } else if (ch != null && !ch.isEmpty()) {
×
137
            return ch.get(0).getOnlyEment();
×
138
        }
139
        throw new RuntimeException("There are " + size() + " elements in this tree.");
×
140
    }
141

142
    public boolean isLeaf() {
143
        return children == null;
×
144
    }
145

146

147
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc