1   package com.trendmicro.grid.acl.ds.cache.commands;
2   
3   import com.trendmicro.grid.acl.ds.cache.CacheDestination;
4   import com.trendmicro.grid.acl.ds.cache.CacheSettings;
5   import org.slf4j.Logger;
6   import org.slf4j.LoggerFactory;
7   
8   import java.util.ArrayList;
9   import java.util.Collection;
10  import java.util.Map;
11  
12  /**
13   * Removes a bunch of keys from a cache destination.
14   *
15   * @author juergen_kellerer, 2011-03-25
16   * @version 1.0
17   */
18  public class RemoveCommand<K, V> extends AbstractCommand<K, V> {
19  
20  	private static final Logger log = LoggerFactory.getLogger(RemoveCommand.class);
21  
22  	private CacheDestination<K, ?>[] cacheDestinations;
23  
24  	/**
25  	 * Constructs the command.
26  	 *
27  	 * @param keys			 the keys to remove.
28  	 * @param cacheDestination the destination(s) to remove the keys from.
29  	 */
30  	public RemoveCommand(Collection<K> keys, CacheDestination<K, ?>... cacheDestination) {
31  		super(new ArrayList<K>(keys));
32  		// Copying the keys as the removal may be async. and external keys collection may change.
33  
34  		cacheDestinations = cacheDestination;
35  	}
36  
37  	/**
38  	 * Returns this command as batch command.
39  	 *
40  	 * @return this command as batch command.
41  	 */
42  	public BatchCommand<K, V> asBatchCommand() {
43  		return new BatchCommand<K, V>(keys, this, cacheDestinations);
44  	}
45  
46  	protected void removeKeys() {
47  		for (CacheDestination<K, ?> destination : cacheDestinations)
48  			destination.removeAll(keys);
49  	}
50  
51  	/**
52  	 * {@inheritDoc}
53  	 */
54  	@Override
55  	public Map<K, V> call() throws Exception {
56  		if (!CacheSettings.DISABLED)
57  			removeKeys();
58  		return keysToCallResult();
59  	}
60  }