Skip to content

Commit

Permalink
add concurrent hash set
Browse files Browse the repository at this point in the history
  • Loading branch information
息羽 committed Feb 18, 2016
1 parent 67da963 commit 30101c9
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/main/java/darks/grid/network/discovery/TCPPING.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import darks.grid.GridException;
import darks.grid.GridRuntime;
import darks.grid.manager.GridNodesManager;
import darks.grid.utils.ParamsUtils;
Expand Down Expand Up @@ -92,7 +93,14 @@ public void setHosts(String hosts)
this.hosts = hosts;
if (hosts != null)
{
tryAddressList = ParamsUtils.parseAddress(hosts);
try
{
tryAddressList = ParamsUtils.parseAddress(hosts);
}
catch (Exception e)
{
throw new GridException("Invalid TCPPING hosts:" + hosts, e);
}
}
}

Expand Down
124 changes: 124 additions & 0 deletions src/main/java/darks/grid/utils/ConcurrentHashSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/**
*
* Copyright 2015 The Darks Grid Project (Liu lihua)
*
* 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 darks.grid.utils;

import java.io.Serializable;
import java.util.AbstractSet;
import java.util.Collection;
import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;

public class ConcurrentHashSet<E> extends AbstractSet<E> implements Serializable {

/**
*
*/
private static final long serialVersionUID = -4868544861254524679L;

private static final Object PRESENT = new Object();

private transient ConcurrentHashMap<E, Object> data = null;

public ConcurrentHashSet() {
data = new ConcurrentHashMap<E, Object>();
}

public ConcurrentHashSet(int initialCapacity) {
data = new ConcurrentHashMap<E, Object>(initialCapacity);
}

public ConcurrentHashSet(Collection<E> c) {
data = new ConcurrentHashMap<E, Object>(c.size());
addAll(c);
}


@Override
public Iterator<E> iterator() {
return data.keySet().iterator();
}

@Override
public int size() {
return data.size();
}

@Override
public boolean equals(Object o) {
if (o != null && o instanceof ConcurrentHashSet) {
ConcurrentHashSet<E> os = (ConcurrentHashSet<E>) o;
return data.equals(os.data);
}
return false;
}

@Override
public int hashCode() {
return data.hashCode();
}

@Override
public boolean isEmpty() {
return data.isEmpty();
}

@Override
public boolean contains(Object o) {
return data.containsKey(o);
}

@Override
public boolean add(E e) {
return data.put(e, PRESENT) == null;
}

@Override
public boolean remove(Object o) {
return data.remove(o) != null;
}

@Override
public void clear() {
data.clear();
}

private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException {
// Write out any hidden serialization magic
s.defaultWriteObject();

// Write out size
s.writeInt(data.size());

// Write out all elements in the proper order.
for (E e : data.keySet())
s.writeObject(e);
}

private void readObject(java.io.ObjectInputStream s) throws java.io.IOException,
ClassNotFoundException {
// Read in any hidden serialization magic
s.defaultReadObject();

int size = s.readInt();
data = new ConcurrentHashMap<E, Object>(size);

// Read in all elements in the proper order.
for (int i = 0; i < size; i++) {
E e = (E) s.readObject();
data.put(e, PRESENT);
}
}
}

0 comments on commit 30101c9

Please sign in to comment.