All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.jsync.shareddata.SharedData Maven / Gradle / Ivy

There is a newer version: 1.10.13
Show newest version
/*
 * Copyright (c) 2011-2013 The original author or authors
 * ------------------------------------------------------
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Apache License v2.0 which accompanies this distribution.
 *
 *     The Eclipse Public License is available at
 *     http://www.eclipse.org/legal/epl-v10.html
 *
 *     The Apache License v2.0 is available at
 *     http://www.opensource.org/licenses/apache2.0.php
 *
 * You may elect to redistribute this code under either of these licenses.
 */

package io.jsync.shareddata;

import io.jsync.logging.Logger;
import io.jsync.logging.impl.LoggerFactory;
import io.jsync.shareddata.impl.SharedMap;
import io.jsync.shareddata.impl.SharedSet;

import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * Sometimes it is desirable to share immutable data between different event loops, for example to implement a
 * cache of data.

* This class allows instances of shared data structures to be looked up and used from different event loops.

* The data structures themselves will only allow certain data types to be stored into them. This shields you from * worrying about any thread safety issues might occur if mutable objects were shared between event loops.

* The following types can be stored in a shareddata data structure:

*

 *   {@link String}
 *   {@link Integer}
 *   {@link Long}
 *   {@link Double}
 *   {@link Float}
 *   {@link Short}
 *   {@link Byte}
 *   {@link Character}
 *   {@code byte[]} - this will be automatically copied, and the copy will be stored in the structure.
 *   {@link io.jsync.buffer.Buffer} - this will be automatically copied, and the copy will be stored in the
 *   structure.
 * 
*

*

* Instances of this class are thread-safe.

* * @author Tim Fox */ public class SharedData { private static final Logger log = LoggerFactory.getLogger(SharedData.class); private ConcurrentMap> maps = new ConcurrentHashMap<>(); private ConcurrentMap> sets = new ConcurrentHashMap<>(); /** * Return a {@code Map} with the specific {@code name}. All invocations of this method with the same value of {@code name} * are guaranteed to return the same {@code Map} instance.

*/ public ConcurrentSharedMap getMap(String name) { SharedMap map = (SharedMap) maps.get(name); if (map == null) { map = new SharedMap<>(); SharedMap prev = maps.putIfAbsent(name, map); if (prev != null) { map = prev; } } return map; } /** * Return a {@code Set} with the specific {@code name}. All invocations of this method with the same value of {@code name} * are guaranteed to return the same {@code Set} instance.

*/ public Set getSet(String name) { SharedSet set = (SharedSet) sets.get(name); if (set == null) { set = new SharedSet<>(); SharedSet prev = sets.putIfAbsent(name, set); if (prev != null) { set = prev; } } return set; } /** * Remove the {@code Map} with the specific {@code name}. */ public boolean removeMap(Object name) { return maps.remove(name) != null; } /** * Remove the {@code Set} with the specific {@code name}. */ public boolean removeSet(Object name) { return sets.remove(name) != null; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy