]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.g2d/src/org/simantics/g2d/utils/TextSegment.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / utils / TextSegment.java
index 2179dd2c6783a31dffe1303ccdc12f404ea5bf5b..61fda9940439dfe1ff46ebd7b638fc3166360fff 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.g2d.utils;\r
-\r
-import java.io.Serializable;\r
-\r
-/**\r
- * @author Tuukka Lehtonen\r
- */\r
-public class TextSegment implements Serializable {\r
-\r
-    private static final long                 serialVersionUID = 1428883453597424952L;\r
-\r
-    public static transient final TextSegment EMPTY            = new TextSegment(0, 0);\r
-\r
-    private final int start;\r
-    private final int length;\r
-\r
-    public static TextSegment whole(String s) {\r
-        return new TextSegment(0, s.length());\r
-    }\r
-\r
-    public static TextSegment single(int pos) {\r
-        return new TextSegment(pos, 1);\r
-    }\r
-\r
-    public static TextSegment before(int pos) {\r
-        return new TextSegment(0, pos);\r
-    }\r
-\r
-    public TextSegment(int start, int length) {\r
-        this.start = start;\r
-        this.length = length;\r
-    }\r
-\r
-    public int start() {\r
-        return start;\r
-    }\r
-\r
-    public int end() {\r
-        return start + length;\r
-    }\r
-\r
-    public int length() {\r
-        return length;\r
-    }\r
-\r
-    public boolean isEmpty() {\r
-        return length == 0;\r
-    }\r
-\r
-    public boolean isWhole(String s) {\r
-        if (s == null)\r
-            return false;\r
-        return start == 0 && length == s.length();\r
-    }\r
-\r
-    public boolean atStart(int pos) {\r
-        return start == pos;\r
-    }\r
-\r
-    public boolean atEnd(int pos) {\r
-        return end() == pos;\r
-    }\r
-\r
-    public boolean existsIn(String s) {\r
-        int l = s != null ? s.length() : 0;\r
-        if (start > l)\r
-            return false;\r
-        int end = start + length;\r
-        if (end > l)\r
-            return false;\r
-        return true;\r
-    }\r
-\r
-    public String removeFrom(String s) {\r
-        if (!existsIn(s))\r
-            throw new IllegalArgumentException("segment " + this + " is out of bounds in string '" + s + "'");\r
-        if (length == 0)\r
-            return s;\r
-        if (start == 0)\r
-            return s.substring(length);\r
-        return s.substring(0, start) + s.substring(start + length);\r
-    }\r
-\r
-    public String before(String s) {\r
-        if (start == 0)\r
-            return "";\r
-        return s.substring(0, start);\r
-    }\r
-\r
-    public String after(String s) {\r
-        if (end() == s.length())\r
-            return "";\r
-        return s.substring(start, start + length);\r
-    }\r
-\r
-    public String of(String s) {\r
-        return s.substring(start, start + length);\r
-    }\r
-\r
-    public TextSegment beforeStart() {\r
-        if (start == 0)\r
-            return EMPTY;\r
-        return new TextSegment(0, start);\r
-    }\r
-\r
-    public TextSegment afterEnd(String s) {\r
-        int l = s.length();\r
-        int end = end();\r
-        if (end > l)\r
-            throw new IllegalArgumentException("end " + end + " is beyond string '" + s + "'");\r
-        if (end == l)\r
-            return EMPTY;\r
-        return new TextSegment(end, l - end);\r
-    }\r
-\r
-    public TextSegment prepend(int count) {\r
-        if (count == 0)\r
-            return this;\r
-        int newStart = start - count;\r
-        if (newStart < 0)\r
-            throw new IllegalArgumentException("prepending " + this + " by " + count + " procudes invalid segment start " + newStart);\r
-        int newLength = length + count;\r
-        if (newLength < 0)\r
-            throw new IllegalArgumentException("prepending " + this + " by " + count + " produces negative length: " + newLength);\r
-        return new TextSegment(start - count, newLength);\r
-    }\r
-\r
-    public TextSegment append(int count) {\r
-        if (count == 0)\r
-            return this;\r
-        int newLength = length + count;\r
-        if (newLength < 0)\r
-            throw new IllegalArgumentException("appending by " + count + " produces negative length: " + newLength);\r
-        return new TextSegment(start, newLength);\r
-    }\r
-\r
-    public TextSegment extendToEnd(String s) {\r
-        int l = s.length();\r
-        int end = end();\r
-        if (end > l)\r
-            throw new IllegalArgumentException("end " + end + " is beyond string '" + s + "'");\r
-        if (end == l)\r
-            return this;\r
-        return new TextSegment(start, l - start);\r
-    }\r
-\r
-    @Override\r
-    public int hashCode() {\r
-        final int prime = 31;\r
-        int result = 1;\r
-        result = prime * result + length;\r
-        result = prime * result + start;\r
-        return result;\r
-    }\r
-\r
-    @Override\r
-    public boolean equals(Object obj) {\r
-        if (this == obj)\r
-            return true;\r
-        if (obj == null)\r
-            return false;\r
-        if (getClass() != obj.getClass())\r
-            return false;\r
-        TextSegment other = (TextSegment) obj;\r
-        if (length != other.length)\r
-            return false;\r
-        if (start != other.start)\r
-            return false;\r
-        return true;\r
-    }\r
-\r
-    @Override\r
-    public String toString() {\r
-        return "[" + start + "," + (start + length) + "]"; \r
-    }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.g2d.utils;
+
+import java.io.Serializable;
+
+/**
+ * @author Tuukka Lehtonen
+ */
+public class TextSegment implements Serializable {
+
+    private static final long                 serialVersionUID = 1428883453597424952L;
+
+    public static transient final TextSegment EMPTY            = new TextSegment(0, 0);
+
+    private final int start;
+    private final int length;
+
+    public static TextSegment whole(String s) {
+        return new TextSegment(0, s.length());
+    }
+
+    public static TextSegment single(int pos) {
+        return new TextSegment(pos, 1);
+    }
+
+    public static TextSegment before(int pos) {
+        return new TextSegment(0, pos);
+    }
+
+    public TextSegment(int start, int length) {
+        this.start = start;
+        this.length = length;
+    }
+
+    public int start() {
+        return start;
+    }
+
+    public int end() {
+        return start + length;
+    }
+
+    public int length() {
+        return length;
+    }
+
+    public boolean isEmpty() {
+        return length == 0;
+    }
+
+    public boolean isWhole(String s) {
+        if (s == null)
+            return false;
+        return start == 0 && length == s.length();
+    }
+
+    public boolean atStart(int pos) {
+        return start == pos;
+    }
+
+    public boolean atEnd(int pos) {
+        return end() == pos;
+    }
+
+    public boolean existsIn(String s) {
+        int l = s != null ? s.length() : 0;
+        if (start > l)
+            return false;
+        int end = start + length;
+        if (end > l)
+            return false;
+        return true;
+    }
+
+    public String removeFrom(String s) {
+        if (!existsIn(s))
+            throw new IllegalArgumentException("segment " + this + " is out of bounds in string '" + s + "'");
+        if (length == 0)
+            return s;
+        if (start == 0)
+            return s.substring(length);
+        return s.substring(0, start) + s.substring(start + length);
+    }
+
+    public String before(String s) {
+        if (start == 0)
+            return "";
+        return s.substring(0, start);
+    }
+
+    public String after(String s) {
+        if (end() == s.length())
+            return "";
+        return s.substring(start, start + length);
+    }
+
+    public String of(String s) {
+        return s.substring(start, start + length);
+    }
+
+    public TextSegment beforeStart() {
+        if (start == 0)
+            return EMPTY;
+        return new TextSegment(0, start);
+    }
+
+    public TextSegment afterEnd(String s) {
+        int l = s.length();
+        int end = end();
+        if (end > l)
+            throw new IllegalArgumentException("end " + end + " is beyond string '" + s + "'");
+        if (end == l)
+            return EMPTY;
+        return new TextSegment(end, l - end);
+    }
+
+    public TextSegment prepend(int count) {
+        if (count == 0)
+            return this;
+        int newStart = start - count;
+        if (newStart < 0)
+            throw new IllegalArgumentException("prepending " + this + " by " + count + " procudes invalid segment start " + newStart);
+        int newLength = length + count;
+        if (newLength < 0)
+            throw new IllegalArgumentException("prepending " + this + " by " + count + " produces negative length: " + newLength);
+        return new TextSegment(start - count, newLength);
+    }
+
+    public TextSegment append(int count) {
+        if (count == 0)
+            return this;
+        int newLength = length + count;
+        if (newLength < 0)
+            throw new IllegalArgumentException("appending by " + count + " produces negative length: " + newLength);
+        return new TextSegment(start, newLength);
+    }
+
+    public TextSegment extendToEnd(String s) {
+        int l = s.length();
+        int end = end();
+        if (end > l)
+            throw new IllegalArgumentException("end " + end + " is beyond string '" + s + "'");
+        if (end == l)
+            return this;
+        return new TextSegment(start, l - start);
+    }
+
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + length;
+        result = prime * result + start;
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj)
+            return true;
+        if (obj == null)
+            return false;
+        if (getClass() != obj.getClass())
+            return false;
+        TextSegment other = (TextSegment) obj;
+        if (length != other.length)
+            return false;
+        if (start != other.start)
+            return false;
+        return true;
+    }
+
+    @Override
+    public String toString() {
+        return "[" + start + "," + (start + length) + "]"; 
+    }
+
+}