1 /*
2 * (C) Copyright 1989-2011 Trend Micro, Inc.
3 * All Rights Reserved.
4 *
5 * This program is an unpublished copyrighted work which is proprietary
6 * to Trend Micro, Inc. and contains confidential information that is not
7 * to be reproduced or disclosed to any other person or entity without
8 * prior written consent from Trend Micro, Inc. in each and every instance.
9 *
10 * WARNING: Unauthorized reproduction of this program as well as
11 * unauthorized preparation of derivative works based upon the
12 * program or distribution of copies by sale, rental, lease or
13 * lending are violations of federal copyright laws and state trade
14 * secret laws, punishable by civil and criminal penalties.
15 */
16
17 package com.trendmicro.grid.acl.ds.cache.commands;
18
19 import com.trendmicro.grid.acl.ds.cache.CacheDestination;
20 import com.trendmicro.grid.acl.ds.cache.CacheSettings;
21
22 import java.util.Map;
23
24 /**
25 * Writes the given results to the cache.
26 *
27 * @author juergen_kellerer, 2011-03-31
28 * @version 1.0
29 */
30 public class CacheResultsCommand<K, V> extends AbstractCommand<K, V> {
31
32 protected Map<K, V> values;
33 protected CacheDestination<K, V> destination;
34 protected CacheDestination.Mode mode;
35
36 /**
37 * Constructs the command.
38 *
39 * @param values the values to write.
40 * @param destination the destination(s) to initiate a batch on.
41 * @param mode the write mode to use.
42 */
43 public CacheResultsCommand(Map<K, V> values, CacheDestination<K, V> destination, CacheDestination.Mode mode) {
44 super(null);
45
46 // Important: We need to copy the data as the values may get used asynchronously
47 // and external modifications must be avoided.
48 this.values = new NonUniqueMap<K, V>(values.size());
49 this.values.putAll(values);
50 keys = this.values.keySet();
51
52 this.destination = destination;
53 this.mode = mode;
54 }
55
56 /**
57 * Returns this command as batch command.
58 *
59 * @return this command as batch command.
60 */
61 public BatchCommand<K, V> asBatchCommand() {
62 return new BatchCommand<K, V>(keys, this, destination);
63 }
64
65 /**
66 * {@inheritDoc}
67 */
68 @Override
69 public Map<K, V> call() throws Exception {
70 if (!CacheSettings.DISABLED) destination.putAll(values, mode);
71
72 return values;
73 }
74 }