Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 98bb743c5218220d3bd003ec5dcc501ff8dd20de (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
package org.eclipse.debug.internal.ui.views.console;

/**********************************************************************
Copyright (c) 2000, 2002 IBM Corp.  All rights reserved.
This file is 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
**********************************************************************/

import org.eclipse.jface.text.TypedRegion;

/**
 * A partition from an input/output stream connected to the console. 
 */
public abstract class StreamPartition extends TypedRegion {
	
	/**
	 * Stream identifier
	 */
	private String fStreamIdentifier;
	
	public StreamPartition(String streamIdentifier, int offset, int length, String type) {
		super(offset, length, type);
		fStreamIdentifier = streamIdentifier;
	}
	
	
	/**
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	public boolean equals(Object partition) {
		if (super.equals(partition)) {
			fStreamIdentifier.equals(((StreamPartition)partition).getStreamIdentifier());
		}
		return false;
	}

	/**
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		return super.hashCode() + fStreamIdentifier.hashCode();
	}

	/**
	 * Returns this partition's stream identifier
	 * 
	 * @return this partition's stream identifier
 	 */
	public String getStreamIdentifier() {
		return fStreamIdentifier;
	}
	
	/**
	 * Returns whether this partition is allowed to be combined with the
	 * given partition.
	 * 
	 * @param partition
	 * @return boolean
	 */
	public boolean canBeCombinedWith(StreamPartition partition) {
		int start = getOffset();
		int end = start + getLength();
		int otherStart = partition.getOffset();
		int otherEnd = otherStart + partition.getLength();
		boolean overlap = (otherStart >= start && otherStart <= end) || (start >= otherStart && start <= otherEnd);
		return overlap && getType().equals(partition.getType()) && getStreamIdentifier().equals(partition.getStreamIdentifier());
	}
	
	/**
	 * Returns a new partition representing this and the given parition
	 * combined.
	 * 
	 * @param partition
	 * @return partition
 	 */
	public StreamPartition combineWith(StreamPartition partition) {
		int start = getOffset();
		int end = start + getLength();
		int otherStart = partition.getOffset();
		int otherEnd = otherStart + partition.getLength();
		int theStart = Math.min(start, otherStart);
		int theEnd = Math.max(end, otherEnd);
		return createNewPartition(getStreamIdentifier(), theStart, theEnd - theStart);
	}
	
	/**
	 * Creates a new patition of this type with the given color, offset, 
	 * and length.
	 * 
	 * @param streamIdentifer
	 * @param offset
	 * @param length
	 * @return ColorPartition
	 */
	public abstract StreamPartition createNewPartition(String streamIdentifier, int offset, int length);
}

Back to the top