1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.g2d.utils;
14 import java.io.Serializable;
17 * @author Tuukka Lehtonen
19 public class TextSegment implements Serializable {
21 private static final long serialVersionUID = 1428883453597424952L;
23 public static transient final TextSegment EMPTY = new TextSegment(0, 0);
25 private final int start;
26 private final int length;
28 public static TextSegment whole(String s) {
29 return new TextSegment(0, s.length());
32 public static TextSegment single(int pos) {
33 return new TextSegment(pos, 1);
36 public static TextSegment before(int pos) {
37 return new TextSegment(0, pos);
40 public TextSegment(int start, int length) {
50 return start + length;
57 public boolean isEmpty() {
61 public boolean isWhole(String s) {
64 return start == 0 && length == s.length();
67 public boolean atStart(int pos) {
71 public boolean atEnd(int pos) {
75 public boolean existsIn(String s) {
76 int l = s != null ? s.length() : 0;
79 int end = start + length;
85 public String removeFrom(String s) {
87 throw new IllegalArgumentException("segment " + this + " is out of bounds in string '" + s + "'");
91 return s.substring(length);
92 return s.substring(0, start) + s.substring(start + length);
95 public String before(String s) {
98 return s.substring(0, start);
101 public String after(String s) {
102 if (end() == s.length())
104 return s.substring(start, start + length);
107 public String of(String s) {
108 return s.substring(start, start + length);
111 public TextSegment beforeStart() {
114 return new TextSegment(0, start);
117 public TextSegment afterEnd(String s) {
121 throw new IllegalArgumentException("end " + end + " is beyond string '" + s + "'");
124 return new TextSegment(end, l - end);
127 public TextSegment prepend(int count) {
130 int newStart = start - count;
132 throw new IllegalArgumentException("prepending " + this + " by " + count + " procudes invalid segment start " + newStart);
133 int newLength = length + count;
135 throw new IllegalArgumentException("prepending " + this + " by " + count + " produces negative length: " + newLength);
136 return new TextSegment(start - count, newLength);
139 public TextSegment append(int count) {
142 int newLength = length + count;
144 throw new IllegalArgumentException("appending by " + count + " produces negative length: " + newLength);
145 return new TextSegment(start, newLength);
148 public TextSegment extendToEnd(String s) {
152 throw new IllegalArgumentException("end " + end + " is beyond string '" + s + "'");
155 return new TextSegment(start, l - start);
159 public int hashCode() {
160 final int prime = 31;
162 result = prime * result + length;
163 result = prime * result + start;
168 public boolean equals(Object obj) {
173 if (getClass() != obj.getClass())
175 TextSegment other = (TextSegment) obj;
176 if (length != other.length)
178 if (start != other.start)
184 public String toString() {
185 return "[" + start + "," + (start + length) + "]";