-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathFlagSet.java
More file actions
151 lines (139 loc) · 4.38 KB
/
FlagSet.java
File metadata and controls
151 lines (139 loc) · 4.38 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* Copyright © 2016-2025 The LmdbJava Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.lmdbjava;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
/**
* A set of flags, each with a bit mask value. Flags can be combined in a set such that the set has
* a combined bit mask value.
*
* @param <T> The type of flag in the set, must extend {@link MaskedFlag}.
*/
public interface FlagSet<T extends MaskedFlag> extends Iterable<T> {
/**
* The combined mask for this flagSet.
*
* @return The combined mask for this flagSet.
*/
int getMask();
/**
* Combines this {@link FlagSet} with another and returns the combined mask value.
*
* @param other The other {@link FlagSet} to combine with this.
* @return The result of combining the mask of this {@link FlagSet} with the mask of the other
* {@link FlagSet}.
*/
default int getMaskWith(final FlagSet<T> other) {
if (other != null) {
return MaskedFlag.mask(getMask(), other.getMask());
} else {
return getMask();
}
}
/**
* Get the set of flags in this {@link FlagSet}.
*
* @return The set of flags in this {@link FlagSet}.
*/
Set<T> getFlags();
/**
* Tests if flag is non-null and included in this {@link FlagSet}.
*
* @param flag The flag to test.
* @return True if flag is non-null and included in this {@link FlagSet}.
*/
boolean isSet(T flag);
/**
* The number of flags in this set.
*
* @return The number of flags in this set.
*/
int size();
/**
* Tests if at least one of flags are included in this {@link FlagSet}
*
* @param flags The flags to test.
* @return True if at least one of flags are included in this {@link FlagSet}
*/
default boolean areAnySet(final FlagSet<T> flags) {
if (flags == null) {
return false;
} else {
for (final T flag : flags) {
if (isSet(flag)) {
return true;
}
}
}
return false;
}
/**
* Tests if this {@link FlagSet} is empty.
*
* @return True if this {@link FlagSet} is empty.
*/
boolean isEmpty();
/**
* Gets an {@link Iterator} (in no particular order) for the flags in this {@link FlagSet}.
*
* @return The {@link Iterator} (in no particular order) for the flags in this {@link FlagSet}.
*/
@Override
default Iterator<T> iterator() {
return getFlags().iterator();
}
/**
* Convert this {@link FlagSet} to a string for use in toString methods.
*
* @param flagSet The {@link FlagSet} to convert to a string.
* @param <T> The type of the flags in the {@link FlagSet}.
* @return The {@link String} representation of the flagSet.
*/
static <T extends MaskedFlag> String asString(final FlagSet<T> flagSet) {
Objects.requireNonNull(flagSet);
final String flagsStr =
flagSet.getFlags().stream()
.sorted(Comparator.comparing(MaskedFlag::getMask))
.map(MaskedFlag::name)
.collect(Collectors.joining(", "));
return "FlagSet{" + "flags=[" + flagsStr + "], mask=" + flagSet.getMask() + '}';
}
/**
* Compares a {@link FlagSet} to another object
*
* @param flagSet The {@link FlagSet} to compare.
* @param other THe object to compare against the {@link FlagSet}.
* @return True if both arguments implement {@link FlagSet} and contain the same flags.
*/
static boolean equals(final FlagSet<?> flagSet, final Object other) {
if (other instanceof FlagSet) {
final FlagSet<?> flagSet2 = (FlagSet<?>) other;
if (flagSet == flagSet2) {
return true;
} else if (flagSet == null) {
return false;
} else {
return flagSet.getMask() == flagSet2.getMask()
&& Objects.equals(flagSet.getFlags(), flagSet2.getFlags());
}
} else {
return false;
}
}
}