diff --git a/protocol/src/main/java/com/zfoo/protocol/collection/ArrayUtils.java b/protocol/src/main/java/com/zfoo/protocol/collection/ArrayUtils.java index b0284b3c..ad80c00b 100644 --- a/protocol/src/main/java/com/zfoo/protocol/collection/ArrayUtils.java +++ b/protocol/src/main/java/com/zfoo/protocol/collection/ArrayUtils.java @@ -35,6 +35,7 @@ public abstract class ArrayUtils { public static final double[] EMPTY_DOUBLE_ARRAY = new double[0]; public static final char[] EMPTY_CHAR_ARRAY = new char[0]; + public static final Object EMPTY_OBJECT = new Object(); /** * length diff --git a/protocol/src/main/java/com/zfoo/protocol/collection/HashByteSet.java b/protocol/src/main/java/com/zfoo/protocol/collection/HashByteSet.java new file mode 100644 index 00000000..9a751434 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/collection/HashByteSet.java @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * 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 com.zfoo.protocol.collection; + +import io.netty.util.collection.ByteObjectHashMap; + +import java.util.AbstractSet; +import java.util.Iterator; + +/** + * @author godotg + * @version 3.0 + */ +public class HashByteSet extends AbstractSet { + + private final ByteObjectHashMap map; + + public HashByteSet() { + map = new ByteObjectHashMap<>(); + } + + public HashByteSet(int initialCapacity) { + map = new ByteObjectHashMap<>(initialCapacity); + } + + @Override + public Iterator iterator() { + return map.keySet().iterator(); + } + + @Override + public int size() { + return map.size(); + } + + @Override + public boolean isEmpty() { + return map.isEmpty(); + } + + @Override + public boolean contains(Object o) { + return map.containsKey(o); + } + + @Override + public boolean add(Byte e) { + return map.put(e, ArrayUtils.EMPTY_OBJECT) == null; + } + + public boolean add(byte e) { + return map.put(e, ArrayUtils.EMPTY_OBJECT) == null; + } + + @Override + public boolean remove(Object o) { + return map.remove(o) == ArrayUtils.EMPTY_OBJECT; + } + + @Override + public void clear() { + map.clear(); + } + +} diff --git a/protocol/src/main/java/com/zfoo/protocol/collection/HashIntSet.java b/protocol/src/main/java/com/zfoo/protocol/collection/HashIntSet.java new file mode 100644 index 00000000..37fc4990 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/collection/HashIntSet.java @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * 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 com.zfoo.protocol.collection; + +import io.netty.util.collection.IntObjectHashMap; + +import java.util.AbstractSet; +import java.util.Iterator; + +/** + * @author godotg + * @version 3.0 + */ +public class HashIntSet extends AbstractSet { + + private final IntObjectHashMap map; + + public HashIntSet() { + map = new IntObjectHashMap<>(); + } + + public HashIntSet(int initialCapacity) { + map = new IntObjectHashMap<>(initialCapacity); + } + + @Override + public Iterator iterator() { + return map.keySet().iterator(); + } + + @Override + public int size() { + return map.size(); + } + + @Override + public boolean isEmpty() { + return map.isEmpty(); + } + + @Override + public boolean contains(Object o) { + return map.containsKey(o); + } + + @Override + public boolean add(Integer e) { + return map.put(e, ArrayUtils.EMPTY_OBJECT) == null; + } + + public boolean add(int e) { + return map.put(e, ArrayUtils.EMPTY_OBJECT) == null; + } + + @Override + public boolean remove(Object o) { + return map.remove(o) == ArrayUtils.EMPTY_OBJECT; + } + + @Override + public void clear() { + map.clear(); + } + +} diff --git a/protocol/src/main/java/com/zfoo/protocol/collection/HashLongSet.java b/protocol/src/main/java/com/zfoo/protocol/collection/HashLongSet.java new file mode 100644 index 00000000..81593b1b --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/collection/HashLongSet.java @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * 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 com.zfoo.protocol.collection; + +import io.netty.util.collection.LongObjectHashMap; + +import java.util.AbstractSet; +import java.util.Iterator; + +/** + * @author godotg + * @version 3.0 + */ +public class HashLongSet extends AbstractSet { + + private final LongObjectHashMap map; + + public HashLongSet() { + map = new LongObjectHashMap<>(); + } + + public HashLongSet(int initialCapacity) { + map = new LongObjectHashMap<>(initialCapacity); + } + + @Override + public Iterator iterator() { + return map.keySet().iterator(); + } + + @Override + public int size() { + return map.size(); + } + + @Override + public boolean isEmpty() { + return map.isEmpty(); + } + + @Override + public boolean contains(Object o) { + return map.containsKey(o); + } + + @Override + public boolean add(Long e) { + return map.put(e, ArrayUtils.EMPTY_OBJECT) == null; + } + + public boolean add(long e) { + return map.put(e, ArrayUtils.EMPTY_OBJECT) == null; + } + + @Override + public boolean remove(Object o) { + return map.remove(o) == ArrayUtils.EMPTY_OBJECT; + } + + @Override + public void clear() { + map.clear(); + } + +} diff --git a/protocol/src/main/java/com/zfoo/protocol/collection/HashShortSet.java b/protocol/src/main/java/com/zfoo/protocol/collection/HashShortSet.java new file mode 100644 index 00000000..2e493ece --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/collection/HashShortSet.java @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * 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 com.zfoo.protocol.collection; + +import io.netty.util.collection.ShortObjectHashMap; + +import java.util.AbstractSet; +import java.util.Iterator; + +/** + * @author godotg + * @version 3.0 + */ +public class HashShortSet extends AbstractSet { + + private final ShortObjectHashMap map; + + public HashShortSet() { + map = new ShortObjectHashMap<>(); + } + + public HashShortSet(int initialCapacity) { + map = new ShortObjectHashMap<>(initialCapacity); + } + + @Override + public Iterator iterator() { + return map.keySet().iterator(); + } + + @Override + public int size() { + return map.size(); + } + + @Override + public boolean isEmpty() { + return map.isEmpty(); + } + + @Override + public boolean contains(Object o) { + return map.containsKey(o); + } + + @Override + public boolean add(Short e) { + return map.put(e, ArrayUtils.EMPTY_OBJECT) == null; + } + + public boolean add(short e) { + return map.put(e, ArrayUtils.EMPTY_OBJECT) == null; + } + + @Override + public boolean remove(Object o) { + return map.remove(o) == ArrayUtils.EMPTY_OBJECT; + } + + @Override + public void clear() { + map.clear(); + } + +}