Merge branch 'master' into develop
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / services / IndexedForest.java
index 5248299..e2e9cb8 100644 (file)
@@ -18,6 +18,7 @@
 package com.owncloud.android.files.services;
 
 import android.accounts.Account;
+import android.util.Pair;
 
 import com.owncloud.android.datamodel.OCFile;
 
@@ -88,10 +89,14 @@ public class IndexedForest<V> {
         public void removeChild(Node<V> removed) {
             mChildren.remove(removed);
         }
+
+        public void clearPayload() {
+            mPayload = null;
+        }
     }
 
 
-    public /* synchronized */ String putIfAbsent(Account account, String remotePath, V value) {
+    public /* synchronized */ Pair<String, String> putIfAbsent(Account account, String remotePath, V value) {
         String targetKey = buildKey(account, remotePath);
         Node<V> valuedNode = new Node(targetKey, value);
         mMap.putIfAbsent(
@@ -121,12 +126,31 @@ public class IndexedForest<V> {
             currentNode = parentNode;
         }
 
-        return targetKey;
+        String linkedTo = OCFile.ROOT_PATH;
+        if (linked) {
+            linkedTo = parentNode.getKey().substring(account.name.length());
+        }
+        return new Pair<String, String>(targetKey, linkedTo);
     };
 
-    public /* synchronized */ V remove(Account account, String remotePath) {
+
+    public Pair<V, String> removePayload(Account account, String remotePath) {
+        String targetKey = buildKey(account, remotePath);
+        Node<V> target = mMap.get(targetKey);
+        if (target != null) {
+            target.clearPayload();
+            if (!target.hasChildren()) {
+                return remove(account, remotePath);
+            }
+        }
+        return new Pair<V, String>(null, null);
+    }
+
+
+    public /* synchronized */ Pair<V, String> remove(Account account, String remotePath) {
         String targetKey = buildKey(account, remotePath);
         Node<V> firstRemoved = mMap.remove(targetKey);
+        String unlinkedFrom = null;
 
         if (firstRemoved != null) {
             /// remove children
@@ -135,23 +159,25 @@ public class IndexedForest<V> {
             /// remove ancestors if only here due to firstRemoved
             Node<V> removed = firstRemoved;
             Node<V> parent = removed.getParent();
+            boolean unlinked = false;
             while (parent != null) {
                 parent.removeChild(removed);
                 if (!parent.hasChildren()) {
                     removed = mMap.remove(parent.getKey());
                     parent = removed.getParent();
                 } else {
-                    parent = null;
+                    break;
                 }
             }
-        }
 
-        if (firstRemoved != null) {
-            return firstRemoved.getPayload();
-        } else {
-            return null;
+            if (parent != null) {
+                unlinkedFrom = parent.getKey().substring(account.name.length());
+            }
+
+            return new Pair<V, String>(firstRemoved.getPayload(), unlinkedFrom);
         }
 
+        return new Pair<V, String>(null, null);
     }
 
     private void removeDescendants(Node<V> removed) {
@@ -178,6 +204,11 @@ public class IndexedForest<V> {
         }
     }
 
+    public V get(Account account, String remotePath) {
+        String key = buildKey(account, remotePath);
+        return get(key);
+    }
+
 
     /**
      * Builds a key to index files