View Javadoc

1   package cz.cuni.amis.pogamut.ut2004.bot.impl;
2   
3   import java.util.ArrayList;
4   import java.util.Collections;
5   import java.util.HashMap;
6   import java.util.List;
7   import java.util.Map;
8   
9   import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.Configuration;
10  
11  /**
12   * Provides abstraction over the {@link UT2004Bot} name in the game allowing to append "extra infos" to it.
13   * @author Jimmy
14   */
15  public class UT2004BotName {
16  
17  	private UT2004Bot bot;
18  	
19  	private String base;
20  
21  	private Map<String, String> infos = new HashMap<String, String>();
22  	
23  	private String nullKeyValue = null;
24  	
25  	public UT2004BotName(UT2004Bot bot, String base) {
26  		this.bot = bot;
27  		this.base = base;
28  	}
29  	
30  	public void setNameBase(String base) {
31  		this.base = base;
32  		updateName();
33  	}
34  	
35  	public void setTag(String tag) {
36  		setInfo(tag, null);
37  	}
38  	
39  	public void deleteTag(String tag) {
40  		deleteInfo(tag);
41  	}
42  	
43  	/**
44  	 * Alias for {@link #setInfo(String, String)}(null, value), i.e., assigning 'value' to 'NULL KEY'.
45  	 * @param value
46  	 */
47  	public void setInfo(String value) {
48  		setInfo(null, value);
49  	}
50  	
51  	/**
52  	 * @param key CAN BE NULL 
53  	 * @param value CAN BE NULL
54  	 */
55  	public void setInfo(String key, String value) {
56  		if (key == null) {
57  			nullKeyValue = key;
58  		} else {
59  			infos.put(key, value);
60  		}
61  		updateName();
62  	}
63  	
64  	/**
65  	 * @param key CAN BE NULL
66  	 */
67  	public void deleteInfo(String key) {
68  		if (key == null) {
69  			if (nullKeyValue == null) return;
70  			nullKeyValue = null;
71  			updateName();
72  		} else {
73  			if (infos.remove(key) != null) {
74  				updateName();
75  			}
76  		}
77  	}
78  	
79  	public void updateName() {
80  		List<String> keys = new ArrayList<String>(infos.keySet());
81  		Collections.sort(keys);
82  		StringBuffer name = new StringBuffer();
83  		name.append(base);
84  		if (nullKeyValue != null) { 
85  			name.append(" [");
86  			name.append(nullKeyValue);			
87  			name.append("]");
88  		}
89  		for (String key : keys) {
90  			String value = infos.get(key);
91  			name.append(" [");
92  			name.append(key);
93  			if (value != null) {
94  				name.append(": ");
95  				name.append(value);
96  			}
97  			name.append("]");
98  		}
99  		bot.getAct().act(new Configuration().setName(name.toString()));
100 	}
101 	
102 }