OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.content.browser.input; |
| 6 |
| 7 /** |
| 8 * A simple class to set start and end in int type. |
| 9 */ |
| 10 public class Range { |
| 11 private int mStart; |
| 12 private int mEnd; |
| 13 |
| 14 public Range(int start, int end) { |
| 15 set(start, end); |
| 16 } |
| 17 |
| 18 public void set(Range other) { |
| 19 mStart = other.start(); |
| 20 mEnd = other.end(); |
| 21 } |
| 22 |
| 23 public void set(int start, int end) { |
| 24 mStart = Math.min(start, end); |
| 25 mEnd = Math.max(start, end); |
| 26 } |
| 27 |
| 28 public void set(int value) { |
| 29 mStart = value; |
| 30 mEnd = value; |
| 31 } |
| 32 |
| 33 public int start() { |
| 34 return mStart; |
| 35 } |
| 36 |
| 37 public int end() { |
| 38 return mEnd; |
| 39 } |
| 40 |
| 41 public boolean isIn(Range other) { |
| 42 return mStart >= other.mStart && mEnd <= other.mEnd; |
| 43 } |
| 44 |
| 45 public void forceWithin(Range other) { |
| 46 if (mStart < other.mStart) mStart = other.mStart; |
| 47 if (mStart > other.mEnd) mStart = other.mEnd; |
| 48 if (mEnd > other.mEnd) mEnd = other.mEnd; |
| 49 if (mEnd < other.mStart) mEnd = other.mStart; |
| 50 } |
| 51 |
| 52 public static Range fromText(CharSequence text) { |
| 53 return new Range(0, text.length()); |
| 54 } |
| 55 |
| 56 public boolean isEmpty() { |
| 57 return mStart == mEnd; |
| 58 } |
| 59 |
| 60 @Override |
| 61 public boolean equals(Object o) { |
| 62 if (!(o instanceof Range)) return false; |
| 63 if (o == this) return true; |
| 64 Range r = (Range) o; |
| 65 return mStart == r.mStart && mEnd == r.mEnd; |
| 66 } |
| 67 |
| 68 @Override |
| 69 public int hashCode() { |
| 70 final int prime = 31; |
| 71 return prime * mStart + mEnd; |
| 72 } |
| 73 |
| 74 @Override |
| 75 public String toString() { |
| 76 return "[ " + mStart + ", " + mEnd + " ]"; |
| 77 } |
| 78 } |
OLD | NEW |