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.translation;
18  
19  import com.trendmicro.grid.acl.ds.cache.CacheSettings;
20  import com.trendmicro.grid.acl.l0.datatypes.FileIdentifier;
21  import com.trendmicro.grid.acl.l0.datatypes.PackageDetails;
22  import com.trendmicro.grid.acl.l0.datatypes.PackageInformation;
23  
24  import java.util.Map;
25  
26  /**
27   * Uses the given mapping table to translate identifiers to names.
28   *
29   * @author juergen_kellerer, 2011-03-31
30   * @version 1.0
31   */
32  public class IdentifierToNameTranslator<V> extends Translator<FileIdentifier, V, String, V> {
33  
34  	Map<FileIdentifier, String> identifiersToNames;
35  
36  	/**
37  	 * Constructs a new translator with the given mapping table.
38  	 *
39  	 * @param identifiersToNames the mapping table to use.
40  	 */
41  	public IdentifierToNameTranslator(Map<FileIdentifier, String> identifiersToNames) {
42  		this.identifiersToNames = identifiersToNames;
43  	}
44  
45  	/**
46  	 * {@inheritDoc}
47  	 */
48  	@Override
49  	protected String toDelegateKey(FileIdentifier key, V value) {
50  		if (value instanceof PackageDetails) {
51  			PackageInformation info = ((PackageDetails) value).getPackageInformation();
52  			return info == null ? null : info.getName();
53  		}
54  
55  		if (value instanceof PackageInformation) {
56  			return ((PackageInformation) value).getName();
57  		}
58  
59  		return identifiersToNames.get(key);
60  	}
61  
62  	/**
63  	 * {@inheritDoc}
64  	 */
65  	@Override
66  	protected V toDelegateValue(FileIdentifier key, String delegateKey, V delegateValue) {
67  		if (!CacheSettings.DISABLED && key != null && delegateKey != null)
68  			identifiersToNames.put(key, delegateKey);
69  		return delegateValue;
70  	}
71  }