Here you can find the source of substringFromMultiValuedFields(int start, int end, String[] fieldValues, int offsetGap, String interFieldJoiner)
Parameter | Description |
---|---|
start | character offset to start |
end | character offset to end |
fieldValues | array of Strings to process |
offsetGap | offsetGap as typically returned by Analyzer's .getOffsetGap() |
interFieldJoiner | string to use to mark that a substring goes beyond a single field entry |
public static String substringFromMultiValuedFields(int start, int end, String[] fieldValues, int offsetGap, String interFieldJoiner)
//package com.java2s; /*//from ww w.j a va 2 s.c om * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ public class Main { /** * This calculates a substring from an array of StorableFields. * * This attempts to do the best job possible, and at worst will * return an empty string. If the start or end is within a gap, * or before 0 or after the total number of characters, this will * gracefully (blithely?) handle those cases. * * * @param start character offset to start * @param end character offset to end * @param fieldValues array of Strings to process * @param offsetGap offsetGap as typically returned by Analyzer's .getOffsetGap() * @param interFieldJoiner string to use to mark that a substring goes beyond a single * field entry * @return substring, potentially empty, never null. */ public static String substringFromMultiValuedFields(int start, int end, String[] fieldValues, int offsetGap, String interFieldJoiner) { start = (start < 0) ? 0 : start; end = (end < 0) ? 0 : end; if (start > end) { start = end; } int charBase = 0; StringBuilder sb = new StringBuilder(); int lastFieldIndex = 0; int localStart = 0; boolean foundStart = false; //get start for (int fieldIndex = 0; fieldIndex < fieldValues.length; fieldIndex++) { String fString = fieldValues[fieldIndex]; if (start < charBase + fString.length()) { localStart = start - charBase; lastFieldIndex = fieldIndex; foundStart = true; break; } charBase += fString.length() + offsetGap; } if (foundStart == false) { return ""; } //if start occurred in a gap, reset localStart to 0 if (localStart < 0) { sb.append(interFieldJoiner); localStart = 0; } //now append and look for end for (int fieldIndex = lastFieldIndex; fieldIndex < fieldValues.length; fieldIndex++) { String fString = fieldValues[fieldIndex]; if (end <= charBase + fString.length()) { int localEnd = end - charBase; //must be in gap if (charBase > end) { return sb.toString(); } if (fieldIndex != lastFieldIndex) { sb.append(interFieldJoiner); } sb.append(fString.substring(localStart, localEnd)); break; } else { if (fieldIndex != lastFieldIndex) { sb.append(interFieldJoiner); } sb.append(fString.substring(localStart)); localStart = 0; } charBase += fString.length() + offsetGap; } return sb.toString(); } }