~ubuntu-branches/ubuntu/trusty/eclipse-linuxtools/trusty

« back to all changes in this revision

Viewing changes to lttng/org.eclipse.linuxtools.lttng.core/src/org/eclipse/linuxtools/internal/lttng/core/tracecontrol/model/TargetResource.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2012-06-29 12:07:30 UTC
  • Revision ID: package-import@ubuntu.com-20120629120730-bfri1xys1i71dpn6
Tags: upstream-1.0.0
Import upstream version 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************************************************
 
2
 * Copyright (c) 2011 Ericsson
 
3
 * 
 
4
 * All rights reserved. This program and the accompanying materials are
 
5
 * made available under the terms of the Eclipse Public License v1.0 which
 
6
 * accompanies this distribution, and is available at
 
7
 * http://www.eclipse.org/legal/epl-v10.html
 
8
 * 
 
9
 * Contributors:
 
10
 *   Polytechnique Montréal - Initial API and implementation
 
11
 *   Bernd Hufmann - Productification, enhancements and fixes
 
12
 *   
 
13
 *******************************************************************************/
 
14
package org.eclipse.linuxtools.internal.lttng.core.tracecontrol.model;
 
15
 
 
16
import java.util.ArrayList;
 
17
import java.util.Arrays;
 
18
import java.util.List;
 
19
 
 
20
import org.eclipse.linuxtools.internal.lttng.core.LttngConstants;
 
21
import org.eclipse.linuxtools.internal.lttng.core.tracecontrol.model.ProviderResource;
 
22
import org.eclipse.linuxtools.internal.lttng.core.tracecontrol.model.TargetResource;
 
23
import org.eclipse.linuxtools.internal.lttng.core.tracecontrol.model.TraceResource;
 
24
import org.eclipse.rse.core.subsystems.AbstractResource;
 
25
import org.eclipse.rse.core.subsystems.ISubSystem;
 
26
 
 
27
/**
 
28
 * <b><u>TargetResource</u></b>
 
29
 * <p>
 
30
 * This models a remote resource representing a target defined on a particular system.
 
31
 * </p>
 
32
 */
 
33
public class TargetResource extends AbstractResource implements Comparable<TargetResource> {
 
34
 
 
35
    // ------------------------------------------------------------------------
 
36
    // Attributes
 
37
    // ------------------------------------------------------------------------
 
38
    private String fName;
 
39
    private List<TraceResource> fTraces;
 
40
    private ProviderResource fParent;
 
41
    private String fCanCreateNewTrace;
 
42
 
 
43
    // ------------------------------------------------------------------------
 
44
    // Constructors
 
45
    // ------------------------------------------------------------------------
 
46
    /**
 
47
     * Constructor for TargetResource when given fParent subsystem.
 
48
     */
 
49
    public TargetResource(ISubSystem parentSubSystem) {
 
50
        super(parentSubSystem);
 
51
        fCanCreateNewTrace = LttngConstants.Rse_Resource_Action_Enabled;
 
52
        fTraces = new ArrayList<TraceResource>();
 
53
    }
 
54
 
 
55
    // ------------------------------------------------------------------------
 
56
    // Operations
 
57
    // ------------------------------------------------------------------------
 
58
    /**
 
59
     * Returns the name of the target resource.
 
60
     * 
 
61
     * @return String
 
62
     */
 
63
    public String getName() {
 
64
        return fName;
 
65
    }
 
66
 
 
67
    /**
 
68
     * Sets the name of the target resource.
 
69
     * 
 
70
     * @param fName The fName to set
 
71
     */
 
72
    public void setName(String name) {
 
73
        fName = name;
 
74
    }
 
75
 
 
76
    /**
 
77
     * Sets the traces (children).
 
78
     * 
 
79
     * @param newTraces The new traces to set
 
80
     */
 
81
    public void setTraces(TraceResource[] newTraces) {
 
82
        fTraces.clear();
 
83
        fTraces.addAll(Arrays.asList(newTraces));
 
84
    }
 
85
 
 
86
    /**
 
87
     * Gets the traces (children).
 
88
     * 
 
89
     * @return traces (children)
 
90
     */
 
91
    public TraceResource[] getTraces() {
 
92
        TraceResource[] traces = fTraces.toArray(new TraceResource[0]);
 
93
        Arrays.sort(traces);
 
94
        return traces;
 
95
    }
 
96
 
 
97
    /**
 
98
     * Gets the trace for a given name.
 
99
     * 
 
100
     * @param name The name of trace to search for.
 
101
     * @return trace if exists else null
 
102
     */
 
103
    public TraceResource getTrace(String name) {
 
104
        for (TraceResource trace : fTraces) {
 
105
            if (trace.getName().equals(name)) {
 
106
                return trace;
 
107
            }
 
108
        }
 
109
        return null;
 
110
    }
 
111
    
 
112
    /**
 
113
     * Adds a new trace (child) to the existing list of traces.
 
114
     * 
 
115
     * @param trace The new trace to add.
 
116
     */
 
117
    public void addTrace(TraceResource trace) {
 
118
        fTraces.add(trace);
 
119
    }
 
120
 
 
121
    /**
 
122
     * Removes a new trace (child) from the existing list of traces.
 
123
     * 
 
124
     * @param trace The new trace to add.
 
125
     */
 
126
    public void removeTrace(TraceResource trace) {
 
127
        fTraces.remove(trace);
 
128
    }
 
129
    
 
130
    /**
 
131
     * Removes all traces (children).
 
132
     */
 
133
    public void removeAllTraces() {
 
134
        fTraces.clear();
 
135
    }
 
136
 
 
137
    /**
 
138
     * Refreshes target with other traces list. If trace already exists in this
 
139
     * target, reuse the trace from this target and don't override.   
 
140
     * 
 
141
     * @param otherTargets
 
142
     */
 
143
    public void refreshTraces(TraceResource[] otherTraces) {
 
144
        List<TraceResource>  newTraces = new ArrayList<TraceResource>();
 
145
        for (int i = 0; i < otherTraces.length; i++) {
 
146
            boolean added = false;
 
147
            for (TraceResource trace : fTraces) {
 
148
                if (otherTraces[i].equals(trace)) {
 
149
                    newTraces.add(trace);
 
150
                    added = true;
 
151
                    break;
 
152
                }
 
153
            }
 
154
            if (!added) {
 
155
                newTraces.add(otherTraces[i]);
 
156
            }
 
157
        }
 
158
        fTraces = newTraces;
 
159
    }
 
160
 
 
161
    /**
 
162
     * Returns the parent provider resource.
 
163
     * 
 
164
     * @return parent provider resource
 
165
     */
 
166
    public ProviderResource getParent() {
 
167
        return fParent;
 
168
    }
 
169
 
 
170
    /**
 
171
     * Sets the parent provider resource.
 
172
     * @param provider
 
173
     */
 
174
    public void setParent(ProviderResource provider) {
 
175
        fParent = provider;
 
176
    }
 
177
 
 
178
    /**
 
179
     * Returns whether the target is for UST or kernel traces. 
 
180
     * 
 
181
     * @return true if UST, false for kernel 
 
182
     */
 
183
    public boolean isUst() {
 
184
        return fParent.isUst();
 
185
    }
 
186
    
 
187
    /**
 
188
     * Gets property whether target can create new trace or not 
 
189
     * @return fCanCreateNewTrace
 
190
     */
 
191
    public String getCanCreateNewTrace() {
 
192
        return fCanCreateNewTrace;
 
193
    }
 
194
 
 
195
    /*
 
196
     * (non-Javadoc)
 
197
     * @see java.lang.Object#equals(java.lang.Object)
 
198
     */
 
199
    @Override
 
200
    public boolean equals(Object other) {
 
201
 
 
202
        if (this == other) {
 
203
            return true;
 
204
        }
 
205
 
 
206
        // We only check the name because the target name has to be unique
 
207
        if (other instanceof TargetResource) {
 
208
            TargetResource otherTarget = (TargetResource) other;
 
209
            if (fName != null) {
 
210
                return fName.equals(otherTarget.fName);
 
211
            }
 
212
        }
 
213
        return false;
 
214
    }
 
215
 
 
216
    /*
 
217
     * (non-Javadoc)
 
218
     * @see java.lang.Object#hashCode()
 
219
     */
 
220
    @Override 
 
221
    public int hashCode() {
 
222
        // We only use the name because the target name has to be unique
 
223
        return fName.hashCode();
 
224
    }
 
225
 
 
226
    /*
 
227
     * (non-Javadoc)
 
228
     * @see java.lang.Comparable#compareTo(java.lang.Object)
 
229
     */
 
230
    @Override
 
231
    public int compareTo(TargetResource o) {
 
232
        // We only check the name because the trace name has to be unique
 
233
        return fName.toLowerCase().compareTo(o.fName.toLowerCase());
 
234
    }
 
235
    
 
236
    /*
 
237
     * (non-Javadoc)
 
238
     * @see java.lang.Object#toString()
 
239
     */
 
240
    @Override
 
241
    @SuppressWarnings("nls")
 
242
    public String toString() {
 
243
        return "[TargetResource (" + fName + ")]";
 
244
    }
 
245
}