KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > views > console > StreamPartition


1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.debug.internal.ui.views.console;
12
13
14 import org.eclipse.jface.text.TypedRegion;
15
16 /**
17  * A partition from an input/output stream connected to the console.
18  */

19 public abstract class StreamPartition extends TypedRegion {
20     
21     /**
22      * Stream identifier
23      */

24     private String JavaDoc fStreamIdentifier;
25     
26     public StreamPartition(String JavaDoc streamIdentifier, int offset, int length, String JavaDoc type) {
27         super(offset, length, type);
28         fStreamIdentifier = streamIdentifier;
29     }
30     
31     
32     /**
33      * @see java.lang.Object#equals(java.lang.Object)
34      */

35     public boolean equals(Object JavaDoc partition) {
36         if (super.equals(partition)) {
37             fStreamIdentifier.equals(((StreamPartition)partition).getStreamIdentifier());
38         }
39         return false;
40     }
41
42     /**
43      * @see java.lang.Object#hashCode()
44      */

45     public int hashCode() {
46         return super.hashCode() + fStreamIdentifier.hashCode();
47     }
48
49     /**
50      * Returns this partition's stream identifier
51      *
52      * @return this partition's stream identifier
53      */

54     public String JavaDoc getStreamIdentifier() {
55         return fStreamIdentifier;
56     }
57     
58     /**
59      * Returns whether this partition is allowed to be combined with the
60      * given partition.
61      *
62      * @param partition
63      * @return boolean
64      */

65     public boolean canBeCombinedWith(StreamPartition partition) {
66         int start = getOffset();
67         int end = start + getLength();
68         int otherStart = partition.getOffset();
69         int otherEnd = otherStart + partition.getLength();
70         boolean overlap = (otherStart >= start && otherStart <= end) || (start >= otherStart && start <= otherEnd);
71         return overlap && getType().equals(partition.getType()) && getStreamIdentifier().equals(partition.getStreamIdentifier());
72     }
73     
74     /**
75      * Returns a new partition representing this and the given parition
76      * combined.
77      *
78      * @param partition
79      * @return partition
80      */

81     public StreamPartition combineWith(StreamPartition partition) {
82         int start = getOffset();
83         int end = start + getLength();
84         int otherStart = partition.getOffset();
85         int otherEnd = otherStart + partition.getLength();
86         int theStart = Math.min(start, otherStart);
87         int theEnd = Math.max(end, otherEnd);
88         return createNewPartition(getStreamIdentifier(), theStart, theEnd - theStart);
89     }
90     
91     /**
92      * Creates a new patition of this type with the given color, offset,
93      * and length.
94      *
95      * @param streamIdentifer
96      * @param offset
97      * @param length
98      * @return ColorPartition
99      */

100     public abstract StreamPartition createNewPartition(String JavaDoc streamIdentifier, int offset, int length);
101 }
102
Popular Tags