@Override public double cost() { if (usingPath1) { return path1.cost(); } return path2.cost(); }
@Override public List<Link> links() { if (usingPath1) { return path1.links(); } else { return path2.links(); } }
@Override public boolean useBackup() { if (path2 == null || path2.links() == null) { return false; } usingPath1 = !usingPath1; return true; }
@SuppressWarnings("unchecked") private Object transformActualList(Map<String, Object> map) { SortedMap<Integer, Object> sortedMap = new TreeMap<Integer, Object>(); List<Object> list = new ArrayList<Object>(map.size()); int listSize = -1; // convert keys into integer indexes and sort for (Map.Entry<String, Object> entry : map.entrySet()) { Object value = entry.getValue(); int listIndex = DefaultPath.getListIndex(entry.getKey()); if (Types.isListTerminator(value)) { listSize = listSize == -1 ? listIndex : Math.min(listIndex, listSize); continue; } Object transformedValue; if (Types.isSimple(value)) { transformedValue = value; } else if (value instanceof Map) { transformedValue = transformLists((Map<String, Object>) value); } else { throw new IllegalStateException("found strange object in structure: " + value); } sortedMap.put(DefaultPath.getListIndex(entry.getKey()), transformedValue); } // if no listSize was found then something went wrong, but just warn and use whole list found if (listSize == -1) { log.warn("no list terminator found, using all list elements"); } // copy values into list in sorted order int index = 0; for (Object value : sortedMap.values()) { if (index == listSize) { break; } list.add(value); index++; } return list; }
@Test public void test() { DefaultPath root = new DefaultPath((FileSystem) null, "/"); Assert.assertEquals("/", root.toString()); DefaultPath test = new DefaultPath(root, "test"); Assert.assertEquals("/test", test.toString()); DefaultPath qqq = new DefaultPath(test, "qqq"); Assert.assertEquals("/test/qqq", qqq.toString()); Assert.assertEquals("/", test.getParent().toString()); Iterator<Path> it = qqq.iterator(); Assert.assertEquals("test", it.next().toString()); Assert.assertEquals("qqq", it.next().toString()); Assert.assertEquals("test/qqq", qqq.getRoot().relativize(qqq).toString()); it = qqq.getRoot().relativize(qqq).iterator(); Assert.assertEquals("test", it.next().toString()); Assert.assertEquals("qqq", it.next().toString()); }
@Test public void relativize() { DefaultPath root = new DefaultPath((FileSystem) null, "/"); DefaultPath test = new DefaultPath(root, "test"); DefaultPath asd = new DefaultPath(test, "asd"); DefaultPath qwe = new DefaultPath(root, "qwe"); Assert.assertEquals("/test", test.toString()); Assert.assertEquals("/test/asd", asd.toString()); Path rel = test.relativize(asd); Assert.assertFalse(rel.isAbsolute()); Assert.assertEquals("asd", rel.toString()); Assert.assertEquals("../../qwe", asd.relativize(qwe).toString()); DefaultPath l = new DefaultPath((FileSystem) null, "/tmp/qwe"); Assert.assertEquals( "ert", l.relativize(new DefaultPath((FileSystem) null, "/tmp/qwe/ert")).toString()); l = new DefaultPath((FileSystem) null, "/tmp/qwe/ert"); Assert.assertEquals("../../..", l.relativize(root).toString()); }
@SuppressWarnings("unchecked") private Object transformLists(Map<String, Object> map) { // go through nested maps and transform maps into lists where possible if (DefaultPath.isList(map.keySet())) { return transformActualList(map); } // if not a list, then just recursively transform the structure, and also URL-Decode the keys Map<String, Object> newMap = new HashMap<String, Object>(map.size()); for (Map.Entry<String, Object> entry : map.entrySet()) { Object value = entry.getValue(); if (value instanceof Map) { Object transformedValue = transformLists((Map<String, Object>) value); newMap.put(urlDecode(entry.getKey()), transformedValue); } else if (Types.isSimple(value)) { newMap.put(urlDecode(entry.getKey()), entry.getValue()); } else { throw new IllegalStateException("found strange object in structure: " + value); } } return newMap; }
/** * Creates a disjoint path pair from two default paths. * * @param providerId provider identity * @param path1 primary path * @param path2 backup path */ public DefaultDisjointPath(ProviderId providerId, DefaultPath path1, DefaultPath path2) { super(providerId, path1.links(), path1.cost() + path2.cost()); this.path1 = path1; this.path2 = path2; }
@Test public void resolve() { DefaultPath root = new DefaultPath((FileSystem) null, "/"); DefaultPath test = new DefaultPath(root, "test"); Assert.assertEquals(test, root.resolve("test")); Assert.assertEquals(root, test.resolve("..")); Assert.assertEquals("/asd", test.resolve("../asd").toString()); Assert.assertEquals("/test/asd/qwe", test.resolve("asd/qwe").toString()); Assert.assertEquals( "/test/asd/qwe", test.resolve(new DefaultPath(root.getFileSystem(), "asd/qwe")).toString()); Assert.assertEquals( "/test", test.resolve(new DefaultPath(root.getFileSystem(), false, new String[] {"."})).toString()); Assert.assertEquals( "/test", test.resolve(new DefaultPath(test.getFileSystem(), false, new String[] {"."})).toString()); Assert.assertEquals("/test", test.resolve("").toString()); DefaultPath n = (DefaultPath) test.resolve("uuu/qwe"); Assert.assertEquals("/test/uuu/qwe", n.toString()); Assert.assertEquals(3, n.getNameCount()); }