[tx-robot] updated from transifex
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / services / IndexedForest.java
index f3c38fd..8f4ccb6 100644 (file)
@@ -1,4 +1,7 @@
-/* ownCloud Android client application
+/**
+ *   ownCloud Android client application
+ *
+ *   @author David A. Velasco
  *   Copyright (C) 2015 ownCloud Inc.
  *
  *   This program is free software: you can redistribute it and/or modify
@@ -22,10 +25,8 @@ import android.util.Pair;
 
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.lib.common.utils.Log_OC;
-import com.owncloud.android.operations.UploadFileOperation;
 
 import java.io.File;
-import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Set;
@@ -38,8 +39,6 @@ import java.util.concurrent.ConcurrentMap;
  *  A map provides the indexation based in hashing.
  *
  *  A tree is created per account.
- *
- * @author David A. Velasco
  */
 public class IndexedForest<V> {
 
@@ -102,38 +101,45 @@ public class IndexedForest<V> {
     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(
-                targetKey,
-                valuedNode
+        Node<V> previousValue = mMap.putIfAbsent(
+            targetKey,
+            valuedNode
         );
+        if (previousValue != null) {
+            // remotePath already known; not replaced
+            return null;
 
-        String currentPath = remotePath, parentPath = null, parentKey = null;
-        Node<V> currentNode = valuedNode, parentNode = null;
-        boolean linked = false;
-        while (!OCFile.ROOT_PATH.equals(currentPath) && !linked) {
-            parentPath = new File(currentPath).getParent();
-            if (!parentPath.endsWith(OCFile.PATH_SEPARATOR)) {
-                parentPath += OCFile.PATH_SEPARATOR;
+        } else {
+            // value really added
+            String currentPath = remotePath, parentPath = null, parentKey = null;
+            Node<V> currentNode = valuedNode, parentNode = null;
+            boolean linked = false;
+            while (!OCFile.ROOT_PATH.equals(currentPath) && !linked) {
+                parentPath = new File(currentPath).getParent();
+                if (!parentPath.endsWith(OCFile.PATH_SEPARATOR)) {
+                    parentPath += OCFile.PATH_SEPARATOR;
+                }
+                parentKey = buildKey(account, parentPath);
+                parentNode = mMap.get(parentKey);
+                if (parentNode == null) {
+                    parentNode = new Node(parentKey, null);
+                    parentNode.addChild(currentNode);
+                    mMap.put(parentKey, parentNode);
+                } else {
+                    parentNode.addChild(currentNode);
+                    linked = true;
+                }
+                currentPath = parentPath;
+                currentNode = parentNode;
             }
-            parentKey = buildKey(account, parentPath);
-            parentNode = mMap.get(parentKey);
-            if (parentNode == null) {
-                parentNode = new Node(parentKey, null);
-                parentNode.addChild(currentNode);
-                mMap.put(parentKey, parentNode);
-            } else {
-                parentNode.addChild(currentNode);
-                linked = true;
+
+            String linkedTo = OCFile.ROOT_PATH;
+            if (linked) {
+                linkedTo = parentNode.getKey().substring(account.name.length());
             }
-            currentPath = parentPath;
-            currentNode = parentNode;
-        }
 
-        String linkedTo = OCFile.ROOT_PATH;
-        if (linked) {
-            linkedTo = parentNode.getKey().substring(account.name.length());
+            return new Pair<String, String>(targetKey, linkedTo);
         }
-        return new Pair<String, String>(targetKey, linkedTo);
     };
 
 
@@ -213,19 +219,19 @@ public class IndexedForest<V> {
     }
 
 
-    public ConcurrentMap<String, Node<V>> get(Account account){
-        ConcurrentMap<String, Node<V>> accountMap = new ConcurrentHashMap<String, Node<V>>();
+    /**
+     * Remove the elements that contains account as a part of its key
+     * @param account
+     */
+    public void remove(Account account){
         Iterator<String> it = mMap.keySet().iterator();
         while (it.hasNext()) {
             String key = it.next();
             Log_OC.d("IndexedForest", "Number of pending downloads= "  + mMap.size());
             if (key.startsWith(account.name)) {
-                synchronized (accountMap) {
-                    accountMap.putIfAbsent(key, mMap.get(key));
-                }
+                mMap.remove(key);
             }
         }
-        return accountMap;
     }
 
     /**
@@ -238,6 +244,4 @@ public class IndexedForest<V> {
         return account.name + remotePath;
     }
 
-
-
 }