Utilities { public static List<OffsetPosition> mergePositions(List<OffsetPosition> positions1, List<OffsetPosition> positions2) { if (CollectionUtils.isEmpty(positions1)) return positions2; if (CollectionUtils.isEmpty(positions2)) return positions1; Collections.sort(positions1); Collections.sort(positions2); List<OffsetPosition> result = new ArrayList<OffsetPosition>(); for(OffsetPosition pos : positions1) { result.add(pos); } for(OffsetPosition pos : positions2) { if (!result.contains(pos)) result.add(pos); } Collections.sort(result); List<OffsetPosition> finalResult = new ArrayList<OffsetPosition>(); OffsetPosition prevPos = null; for(OffsetPosition pos : result) { if (prevPos == null) { finalResult.add(pos); prevPos = pos; } else { if ( (pos.start >= prevPos.start) && (pos.end <= prevPos.end) ) { } else if (prevPos.end >= pos.start) { prevPos.end = pos.end; } else { prevPos = pos; finalResult.add(pos); } } } return finalResult; }  static boolean deleteDir(File dir); static boolean deleteOldies(File dir, int maxLifeInSeconds); static boolean deleteOldies(File dir, int maxLifeInSeconds, boolean root); static String cleanBody(String text); static String uploadFile(String urlmsg, String path, String name); static BiblioItem cleanZFNMetadata(BiblioItem item); static String getOsNameAndArch(); static boolean stringToBoolean(String value); @SuppressWarnings("rawtypes") static Object launchMethod(Object obj, Object[] args,
			String methodName); @SuppressWarnings("rawtypes") static Object launchMethod(Object obj, Object[] args,
			Class[] paramTypes, String methodName); @SuppressWarnings("rawtypes") static Method getMethod(Object obj, Class[] paramTypes,
			String methodName); static String dateToString(Date pDate, String pFormat); static boolean doubleEquals(double d1, double d2); static boolean doubleEquals(double d1, double d2, double epsilon); static List<OffsetPosition> mergePositions(List<OffsetPosition> positions1,
		List<OffsetPosition> positions2); static List<OffsetPosition> convertStringOffsetToTokenOffsetOld(
		List<OffsetPosition> stringPosition, List<LayoutToken> tokens); static List<OffsetPosition> convertStringOffsetToTokenOffset(
		List<OffsetPosition> stringPosition, List<LayoutToken> tokens); static String punctuationsSub; }
@Test public void testMergePositions1() throws IOException { List<OffsetPosition> position1 = new ArrayList<OffsetPosition>(); List<OffsetPosition> position2 = new ArrayList<OffsetPosition>(); position1.add(new OffsetPosition(0,2)); position2.add(new OffsetPosition(3,5)); List<OffsetPosition> positions = Utilities.mergePositions(position1,position2); assertEquals(positions.size(), 2); assertEquals(positions.get(0).start, 0); assertEquals(positions.get(0).end, 2); assertEquals(positions.get(1).start, 3); assertEquals(positions.get(1).end, 5); }
@Test public void testMergePositions2() throws IOException { List<OffsetPosition> position1 = new ArrayList<OffsetPosition>(); List<OffsetPosition> position2 = new ArrayList<OffsetPosition>(); position1.add(new OffsetPosition(0,2)); position1.add(new OffsetPosition(4,5)); position2.add(new OffsetPosition(3,4)); position2.add(new OffsetPosition(8,10)); List<OffsetPosition> positions = Utilities.mergePositions(position1,position2); assertEquals(positions.size(), 3); assertEquals(positions.get(0).start, 0); assertEquals(positions.get(0).end, 2); assertEquals(positions.get(1).start, 3); assertEquals(positions.get(1).end, 5); assertEquals(positions.get(2).start, 8); assertEquals(positions.get(2).end, 10); }
@Test public void testMergePositionsOverlap() throws IOException { List<OffsetPosition> position1 = new ArrayList<OffsetPosition>(); List<OffsetPosition> position2 = new ArrayList<OffsetPosition>(); position1.add(new OffsetPosition(0,3)); position1.add(new OffsetPosition(5,6)); position1.add(new OffsetPosition(8,9)); position2.add(new OffsetPosition(1,2)); position2.add(new OffsetPosition(3,6)); position2.add(new OffsetPosition(7,10)); List<OffsetPosition> positions= Utilities.mergePositions(position1,position2); assertEquals(positions.size(), 2); assertEquals(positions.get(0).start, 0); assertEquals(positions.get(0).end, 6); assertEquals(positions.get(1).start, 7); assertEquals(positions.get(1).end, 10); }