]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/utils/TextSegment.java
Two rendering glitch fixes for time series charts
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / utils / TextSegment.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.g2d.utils;
13
14 import java.io.Serializable;
15
16 /**
17  * @author Tuukka Lehtonen
18  */
19 public class TextSegment implements Serializable {
20
21     private static final long                 serialVersionUID = 1428883453597424952L;
22
23     public static transient final TextSegment EMPTY            = new TextSegment(0, 0);
24
25     private final int start;
26     private final int length;
27
28     public static TextSegment whole(String s) {
29         return new TextSegment(0, s.length());
30     }
31
32     public static TextSegment single(int pos) {
33         return new TextSegment(pos, 1);
34     }
35
36     public static TextSegment before(int pos) {
37         return new TextSegment(0, pos);
38     }
39
40     public TextSegment(int start, int length) {
41         this.start = start;
42         this.length = length;
43     }
44
45     public int start() {
46         return start;
47     }
48
49     public int end() {
50         return start + length;
51     }
52
53     public int length() {
54         return length;
55     }
56
57     public boolean isEmpty() {
58         return length == 0;
59     }
60
61     public boolean isWhole(String s) {
62         if (s == null)
63             return false;
64         return start == 0 && length == s.length();
65     }
66
67     public boolean atStart(int pos) {
68         return start == pos;
69     }
70
71     public boolean atEnd(int pos) {
72         return end() == pos;
73     }
74
75     public boolean existsIn(String s) {
76         int l = s != null ? s.length() : 0;
77         if (start > l)
78             return false;
79         int end = start + length;
80         if (end > l)
81             return false;
82         return true;
83     }
84
85     public String removeFrom(String s) {
86         if (!existsIn(s))
87             throw new IllegalArgumentException("segment " + this + " is out of bounds in string '" + s + "'");
88         if (length == 0)
89             return s;
90         if (start == 0)
91             return s.substring(length);
92         return s.substring(0, start) + s.substring(start + length);
93     }
94
95     public String before(String s) {
96         if (start == 0)
97             return "";
98         return s.substring(0, start);
99     }
100
101     public String after(String s) {
102         if (end() == s.length())
103             return "";
104         return s.substring(start, start + length);
105     }
106
107     public String of(String s) {
108         return s.substring(start, start + length);
109     }
110
111     public TextSegment beforeStart() {
112         if (start == 0)
113             return EMPTY;
114         return new TextSegment(0, start);
115     }
116
117     public TextSegment afterEnd(String s) {
118         int l = s.length();
119         int end = end();
120         if (end > l)
121             throw new IllegalArgumentException("end " + end + " is beyond string '" + s + "'");
122         if (end == l)
123             return EMPTY;
124         return new TextSegment(end, l - end);
125     }
126
127     public TextSegment prepend(int count) {
128         if (count == 0)
129             return this;
130         int newStart = start - count;
131         if (newStart < 0)
132             throw new IllegalArgumentException("prepending " + this + " by " + count + " procudes invalid segment start " + newStart);
133         int newLength = length + count;
134         if (newLength < 0)
135             throw new IllegalArgumentException("prepending " + this + " by " + count + " produces negative length: " + newLength);
136         return new TextSegment(start - count, newLength);
137     }
138
139     public TextSegment append(int count) {
140         if (count == 0)
141             return this;
142         int newLength = length + count;
143         if (newLength < 0)
144             throw new IllegalArgumentException("appending by " + count + " produces negative length: " + newLength);
145         return new TextSegment(start, newLength);
146     }
147
148     public TextSegment extendToEnd(String s) {
149         int l = s.length();
150         int end = end();
151         if (end > l)
152             throw new IllegalArgumentException("end " + end + " is beyond string '" + s + "'");
153         if (end == l)
154             return this;
155         return new TextSegment(start, l - start);
156     }
157
158     @Override
159     public int hashCode() {
160         final int prime = 31;
161         int result = 1;
162         result = prime * result + length;
163         result = prime * result + start;
164         return result;
165     }
166
167     @Override
168     public boolean equals(Object obj) {
169         if (this == obj)
170             return true;
171         if (obj == null)
172             return false;
173         if (getClass() != obj.getClass())
174             return false;
175         TextSegment other = (TextSegment) obj;
176         if (length != other.length)
177             return false;
178         if (start != other.start)
179             return false;
180         return true;
181     }
182
183     @Override
184     public String toString() {
185         return "[" + start + "," + (start + length) + "]"; 
186     }
187
188 }