Various utility methods to make often-needed tasks more easy and using up less code. In Java code (or JAPE grammars) you may wish to import static gate.Utils. to access these methods without having to qualify them with a class name. In Groovy code, this class can be used as a categor
| 64 | * @author Johann Petrak, Ian Roberts |
| 65 | */ |
| 66 | public class Utils { |
| 67 | /** |
| 68 | * Return the length of the document content covered by an Annotation as an |
| 69 | * int -- if the content is too long for an int, the method will throw |
| 70 | * a GateRuntimeException. Use getLengthLong(SimpleAnnotation ann) if |
| 71 | * this situation could occur. |
| 72 | * @param ann the annotation for which to determine the length |
| 73 | * @return the length of the document content covered by this annotation. |
| 74 | */ |
| 75 | public static int length(SimpleAnnotation ann) { |
| 76 | long len = lengthLong(ann); |
| 77 | if (len > java.lang.Integer.MAX_VALUE) { |
| 78 | throw new GateRuntimeException( |
| 79 | "Length of annotation too big to be returned as an int: "+len); |
| 80 | } else { |
| 81 | return (int)len; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Return the length of the document content covered by an Annotation as a |
| 87 | * long. |
| 88 | * @param ann the annotation for which to determine the length |
| 89 | * @return the length of the document content covered by this annotation. |
| 90 | */ |
| 91 | public static long lengthLong(SimpleAnnotation ann) { |
| 92 | return ann.getEndNode().getOffset() - |
| 93 | ann.getStartNode().getOffset(); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Return the length of the document as an |
| 98 | * int -- if the content is too long for an int, the method will throw a |
| 99 | * GateRuntimeException. Use getLengthLong(Document doc) if |
| 100 | * this situation could occur. |
| 101 | * @param doc the document for which to determine the length |
| 102 | * @return the length of the document content. |
| 103 | */ |
| 104 | public static int length(Document doc) { |
| 105 | long len = doc.getContent().size(); |
| 106 | if (len > java.lang.Integer.MAX_VALUE) { |
| 107 | throw new GateRuntimeException( |
| 108 | "Length of document too big to be returned as an int: "+len); |
| 109 | } else { |
| 110 | return (int)len; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Return the length of the document as a long. |
| 116 | * @param doc the document for which to determine the length |
| 117 | * @return the length of the document content. |
| 118 | */ |
| 119 | public static long lengthLong(Document doc) { |
| 120 | return doc.getContent().size(); |
| 121 | } |
| 122 | |
| 123 | /** |