Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1d60dd5944d104731222e9c1a0eaa352c7dd0997 (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) 2005, 2011 Intel Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Intel Corporation - Initial API and implementation
 *    James Blackburn (Broadcom Corp.)
 *    IBM Corporation
 *******************************************************************************/
package org.eclipse.cdt.core.envvar;

import org.eclipse.cdt.internal.core.SafeStringInterner;
import org.eclipse.cdt.internal.core.envvar.EnvironmentVariableManager;



/**
 * A trivial implementation of {@link IEnvironmentVariable}
 * 
 * @since 3.0
 */
public class EnvironmentVariable implements IEnvironmentVariable, Cloneable {
	protected String fName;
	protected String fValue;
	protected String fDelimiter;
	protected int fOperation;
	
	public EnvironmentVariable(String name, String value, int op, String delimiter) {
		fName = SafeStringInterner.safeIntern(name);
		fOperation = op;
		fValue = SafeStringInterner.safeIntern(value);
		if (delimiter == null)
			fDelimiter = EnvironmentVariableManager.getDefault().getDefaultDelimiter();
		else
			fDelimiter = delimiter;
	}

	protected EnvironmentVariable() {
		fDelimiter = EnvironmentVariableManager.getDefault().getDefaultDelimiter();
	}

	public EnvironmentVariable(String name){
		this(name,null,ENVVAR_REPLACE,null);
	}
	
	public EnvironmentVariable(String name, String value){
		this(name,value,ENVVAR_REPLACE,null);	
	}

	public EnvironmentVariable(String name, String value, String delimiter){
		this(name,value,ENVVAR_REPLACE,delimiter);	
	}
	
	public EnvironmentVariable(IEnvironmentVariable var){
		this(var.getName(),var.getValue(),var.getOperation(),var.getDelimiter());	
	}

	public String getName(){
		return fName;
	}

	public String getValue(){
		return fValue;
	}

	public int getOperation(){
		return fOperation;
	}

	public String getDelimiter(){
		return fDelimiter;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((fDelimiter == null) ? 0 : fDelimiter.hashCode());
		result = prime * result + ((fName == null) ? 0 : fName.hashCode());
		result = prime * result + fOperation;
		result = prime * result + ((fValue == null) ? 0 : fValue.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == this)
			return true;
		if (!(obj instanceof IEnvironmentVariable))
			return super.equals(obj);
		IEnvironmentVariable other = (IEnvironmentVariable)obj;
		if (!equals(fName, other.getName()))
			return false;
		if (!equals(fValue, other.getValue()))
			return false;
		if (!equals(fDelimiter, other.getDelimiter()))
			return false;
		if (fOperation != other.getOperation())
			return false;
		return true;
	}

	// Helper method to check equality of two objects
	private boolean equals(Object obj1, Object obj2) {
		if (obj1 == obj2)
			return true;
		else if (obj1 == null)
			return false;
		else
			return obj1.equals(obj2);
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		if (fName != null)
			sb.append(fName);
		if (fValue != null)
			sb.append("=").append(fValue); //$NON-NLS-1$
		sb.append(" ").append(fDelimiter); //$NON-NLS-1$
		switch (fOperation) {
			case ENVVAR_REPLACE:
				sb.append(" [REPL]"); //$NON-NLS-1$
				break;
			case ENVVAR_REMOVE:
				sb.append(" [REM]"); //$NON-NLS-1$
				break;
			case ENVVAR_PREPEND:
				sb.append(" [PREP]"); //$NON-NLS-1$
				break;
			case ENVVAR_APPEND:
				sb.append(" [APP]"); //$NON-NLS-1$
				break;
			default:
				sb.append(" [NONE]"); //$NON-NLS-1$
				break;
		}
		return sb.toString();
	}

	@Override
	public Object clone(){
		try {
			return super.clone();
		} catch (CloneNotSupportedException e) {
		}
		return null;
	}
}

Back to the top