Skip to main content
summaryrefslogtreecommitdiffstats
blob: 56d8172b7c3b1309f6c5d71cdfb78cdea120f1b5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*******************************************************************************
 * Copyright (c) 2000, 2003 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.internal.core;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.team.core.ISaveContext;

/**
 * Something (as a mark of visible sign) left by a material thing formely present but now 
 * lost or unknown.
 * 
 * TODO: API doc if this class is to remain. Ideally it should replaced by a core mechanism which
 * allows persisting somewhat like IMemento. 
 */
public class SaveContext implements ISaveContext {
	
	private String name;
	
	private String value;
	
	private Map attributes;
	
	private List children = new ArrayList(2);
	
	public SaveContext() {}
	
	public SaveContext(String name, String value) {
		setName(name);
		setValue(value);
	}
	
	public String getAttribute(String name) {
		if(attributes == null) {
			return null;
		}
		return (String)attributes.get(name);
	}
	
	public void putInteger(String key, int n) {
		addAttribute(key, String.valueOf(n));
	}
	
	public void putFloat(String key, float n) {
		addAttribute(key, String.valueOf(n));
	}
		
	public void putString(String key, String n) {
		addAttribute(key, n);
	}
	
	public void putBoolean(String key, boolean n) {
		addAttribute(key, String.valueOf(n));		
	}
	
	public int getInteger(String key) {
		String f = getAttribute(key);
		if(f != null) {
			return new Integer(f).intValue();
		}
		return 0;
	}
	
	public float getFloat(String key) {
		String f = getAttribute(key);
		if(f != null) {
			return new Float(f).floatValue();
		}
		return 0;
	}
		
	public String getString(String key) {
		return getAttribute(key);
	}
	
	public boolean getBoolean(String key) {
		String bool = getAttribute(key);
		if(bool != null) {
			return bool.equals("true") ? true : false; //$NON-NLS-1$
		}
		return true;
	}
	
	public String[] getAttributeNames() {
		if(attributes == null) {
			return new String[0];
		}
		return (String[])attributes.keySet().toArray(new String[attributes.keySet().size()]);
	}
	
	public ISaveContext[] getChildren() {
		return (SaveContext[]) children.toArray(new SaveContext[children.size()]);
	}

	public String getName() {
		return name;
	}
	
	public String getValue() {
		return value;
	}
	
	public void setAttributes(Map map) {
		attributes = map;
	}

	public void setChildren(ISaveContext[] items) {
		children = new ArrayList(Arrays.asList(items));
	}
	
	public void putChild(ISaveContext child) {
		children.add(child);
	}

	public void setName(String string) {
		name = string;
	}

	public void setValue(String string) {
		value = string;
	}

	public void addAttribute(String key, String value) {
		if(attributes == null) {
			attributes = new HashMap();
		}
		attributes.put(key, value);
	}
	
	public String toString() {
		return getName() + " ->" + attributes.toString(); //$NON-NLS-1$
	}

	/* (non-Javadoc)
	 * @see org.eclipse.team.core.ISaveContext#createChild(java.lang.String, java.lang.String)
	 */
	public ISaveContext createChild(String name, String value) {
		return new SaveContext(name, value);
	}
}

Back to the top