|
| 1 | +/* |
| 2 | + * Copyright 2018 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.appulse.encon.common; |
| 18 | + |
| 19 | +import static java.util.stream.Collectors.toSet; |
| 20 | +import static lombok.AccessLevel.PRIVATE; |
| 21 | + |
| 22 | +import java.util.Collection; |
| 23 | +import java.util.Set; |
| 24 | +import java.util.stream.Stream; |
| 25 | + |
| 26 | +import lombok.Getter; |
| 27 | +import lombok.experimental.FieldDefaults; |
| 28 | + |
| 29 | +/** |
| 30 | + * Enumeration of all available distribution capability flags. |
| 31 | + * |
| 32 | + * @since 1.0.0 |
| 33 | + * @author Artem Labazin |
| 34 | + */ |
| 35 | +@FieldDefaults(level = PRIVATE, makeFinal = true) |
| 36 | +public enum DistributionFlag { |
| 37 | + |
| 38 | + /** |
| 39 | + * The node is to be published and part of the global namespace. |
| 40 | + */ |
| 41 | + PUBLISHED(1), |
| 42 | + |
| 43 | + /** |
| 44 | + * The node implements an atom cache (obsolete). |
| 45 | + */ |
| 46 | + ATOM_CACHE(2), |
| 47 | + |
| 48 | + /** |
| 49 | + * The node implements extended (3 × 32 bits) references. |
| 50 | + * This is required today. If not present, the connection is refused. |
| 51 | + */ |
| 52 | + EXTENDED_REFERENCES(4), |
| 53 | + |
| 54 | + /** |
| 55 | + * The node implements distributed process monitoring. |
| 56 | + */ |
| 57 | + DIST_MONITOR(8), |
| 58 | + |
| 59 | + /** |
| 60 | + * The node uses separate tag for functions (lambdas) in the distribution protocol. |
| 61 | + */ |
| 62 | + FUN_TAGS(0x10), |
| 63 | + |
| 64 | + /** |
| 65 | + * The node implements distributed named process monitoring. |
| 66 | + * <p> |
| 67 | + * Not used. |
| 68 | + */ |
| 69 | + DIST_MONITOR_NAME(0x20), |
| 70 | + |
| 71 | + /** |
| 72 | + * The (hidden) node implements atom cache (obsolete). |
| 73 | + * <p> |
| 74 | + * Not supported. |
| 75 | + */ |
| 76 | + HIDDEN_ATOM_CACHE(0x40), |
| 77 | + |
| 78 | + /** |
| 79 | + * The node understand new function tags. |
| 80 | + */ |
| 81 | + NEW_FUN_TAGS(0x80), |
| 82 | + |
| 83 | + /** |
| 84 | + * The node can handle extended pids and ports. This is required today. |
| 85 | + * If not present, the connection is refused. |
| 86 | + */ |
| 87 | + EXTENDED_PIDS_PORTS(0x100), |
| 88 | + |
| 89 | + /** |
| 90 | + * Unknown and not supported tag... |
| 91 | + */ |
| 92 | + EXPORT_PTR_TAG(0x200), |
| 93 | + |
| 94 | + /** |
| 95 | + * The node supports bit binaries. |
| 96 | + */ |
| 97 | + BIT_BINARIES(0x400), |
| 98 | + |
| 99 | + /** |
| 100 | + * The node understands new float format. |
| 101 | + */ |
| 102 | + NEW_FLOATS(0x800), |
| 103 | + |
| 104 | + /** |
| 105 | + * The node supports UTF IO. |
| 106 | + */ |
| 107 | + UNICODE_IO(0x1000), |
| 108 | + |
| 109 | + /** |
| 110 | + * The node implements atom cache in distribution header. |
| 111 | + */ |
| 112 | + DIST_HDR_ATOM_CACHE(0x2000), |
| 113 | + |
| 114 | + /** |
| 115 | + * The node understand the SMALL_ATOM_EXT tag. |
| 116 | + */ |
| 117 | + SMALL_ATOM_TAGS(0x4000), |
| 118 | + |
| 119 | + /** |
| 120 | + * The node understand UTF-8 encoded atoms. |
| 121 | + */ |
| 122 | + UTF8_ATOMS(0x10000), |
| 123 | + |
| 124 | + /** |
| 125 | + * The node supports map data types. |
| 126 | + */ |
| 127 | + MAP_TAG(0x20000), |
| 128 | + |
| 129 | + /** |
| 130 | + * The node creation is big integer. |
| 131 | + */ |
| 132 | + BIG_CREATION(0x40000); |
| 133 | + |
| 134 | + @Getter |
| 135 | + int code; |
| 136 | + |
| 137 | + DistributionFlag (int code) { |
| 138 | + this.code = code; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Converts set of distribution flags to integer number |
| 143 | + * via bitwise inclusive OR operator. |
| 144 | + * |
| 145 | + * @param flags set of distribution flags |
| 146 | + * |
| 147 | + * @return integer representation of flags |
| 148 | + */ |
| 149 | + public static int bitwiseOr (DistributionFlag... flags) { |
| 150 | + return Stream.of(flags) |
| 151 | + .map(DistributionFlag::getCode) |
| 152 | + .reduce(0, (left, right) -> left | right); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Converts set of distribution flags to integer number |
| 157 | + * via bitwise inclusive OR operator. |
| 158 | + * |
| 159 | + * @param flags set of distribution flags |
| 160 | + * |
| 161 | + * @return integer representation of flags |
| 162 | + */ |
| 163 | + public static int bitwiseOr (Collection<DistributionFlag> flags) { |
| 164 | + return flags.stream() |
| 165 | + .map(DistributionFlag::getCode) |
| 166 | + .reduce(0, (left, right) -> left | right); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Parses integer to set of distribution flags via bitwise AND operation. |
| 171 | + * |
| 172 | + * @param number represents a set of distribution flags |
| 173 | + * |
| 174 | + * @return set of parsed distribution flags |
| 175 | + */ |
| 176 | + public static Set<DistributionFlag> parse (int number) { |
| 177 | + return Stream.of(values()) |
| 178 | + .filter(it -> (number & it.getCode()) != 0) |
| 179 | + .collect(toSet()); |
| 180 | + } |
| 181 | +} |
0 commit comments