1 /* ownCloud Android client application
2 * Copyright (C) 2015 ownCloud Inc.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package com
.owncloud
.android
.files
.services
;
20 import android
.accounts
.Account
;
21 import android
.util
.Pair
;
23 import com
.owncloud
.android
.datamodel
.OCFile
;
26 import java
.util
.HashSet
;
27 import java
.util
.Iterator
;
29 import java
.util
.concurrent
.ConcurrentHashMap
;
30 import java
.util
.concurrent
.ConcurrentMap
;
33 * Helper structure to keep the trees of folders containing any file downloading or synchronizing.
35 * A map provides the indexation based in hashing.
37 * A tree is created per account.
39 * @author David A. Velasco
41 public class IndexedForest
<V
> {
43 private ConcurrentMap
<String
, Node
<V
>> mMap
= new ConcurrentHashMap
<String
, Node
<V
>>();
45 private class Node
<V
> {
47 Node
<V
> mParent
= null
;
48 Set
<Node
<V
>> mChildren
= new HashSet
<Node
<V
>>(); // TODO be careful with hash()
51 // payload is optional
52 public Node(String key
, V payload
) {
54 throw new IllegalArgumentException("Argument key MUST NOT be null");
60 public Node
<V
> getParent() {
64 public Set
<Node
<V
>> getChildren() {
68 public String
getKey() {
72 public V
getPayload() {
76 public void addChild(Node
<V
> child
) {
78 child
.setParent(this);
81 private void setParent(Node
<V
> parent
) {
85 public boolean hasChildren() {
86 return mChildren
.size() > 0;
89 public void removeChild(Node
<V
> removed
) {
90 mChildren
.remove(removed
);
95 public /* synchronized */ Pair
<String
, String
> putIfAbsent(Account account
, String remotePath
, V value
) {
96 String targetKey
= buildKey(account
, remotePath
);
97 Node
<V
> valuedNode
= new Node(targetKey
, value
);
103 String currentPath
= remotePath
, parentPath
= null
, parentKey
= null
;
104 Node
<V
> currentNode
= valuedNode
, parentNode
= null
;
105 boolean linked
= false
;
106 while (!OCFile
.ROOT_PATH
.equals(currentPath
) && !linked
) {
107 parentPath
= new File(currentPath
).getParent();
108 if (!parentPath
.endsWith(OCFile
.PATH_SEPARATOR
)) {
109 parentPath
+= OCFile
.PATH_SEPARATOR
;
111 parentKey
= buildKey(account
, parentPath
);
112 parentNode
= mMap
.get(parentKey
);
113 if (parentNode
== null
) {
114 parentNode
= new Node(parentKey
, null
);
115 parentNode
.addChild(currentNode
);
116 mMap
.put(parentKey
, parentNode
);
118 parentNode
.addChild(currentNode
);
121 currentPath
= parentPath
;
122 currentNode
= parentNode
;
125 String linkedTo
= OCFile
.ROOT_PATH
;
127 linkedTo
= parentNode
.getKey().substring(account
.name
.length());
129 return new Pair
<String
, String
>(targetKey
, linkedTo
);
132 public /* synchronized */ Pair
<V
, String
> remove(Account account
, String remotePath
) {
133 String targetKey
= buildKey(account
, remotePath
);
134 Node
<V
> firstRemoved
= mMap
.remove(targetKey
);
135 String unlinkedFrom
= null
;
137 if (firstRemoved
!= null
) {
139 removeDescendants(firstRemoved
);
141 /// remove ancestors if only here due to firstRemoved
142 Node
<V
> removed
= firstRemoved
;
143 Node
<V
> parent
= removed
.getParent();
144 boolean unlinked
= false
;
145 while (parent
!= null
) {
146 parent
.removeChild(removed
);
147 if (!parent
.hasChildren()) {
148 removed
= mMap
.remove(parent
.getKey());
149 parent
= removed
.getParent();
155 if (parent
!= null
) {
156 unlinkedFrom
= parent
.getKey().substring(account
.name
.length());
160 if (firstRemoved
!= null
) {
161 return new Pair
<V
, String
>(firstRemoved
.getPayload(), unlinkedFrom
);
163 return new Pair
<V
, String
>(null
, unlinkedFrom
);
168 private void removeDescendants(Node
<V
> removed
) {
169 Iterator
<Node
<V
>> childrenIt
= removed
.getChildren().iterator();
170 Node
<V
> child
= null
;
171 while (childrenIt
.hasNext()) {
172 child
= childrenIt
.next();
173 mMap
.remove(child
.getKey());
174 removeDescendants(child
);
178 public boolean contains(Account account
, String remotePath
) {
179 String targetKey
= buildKey(account
, remotePath
);
180 return mMap
.containsKey(targetKey
);
183 public /* synchronized */ V
get(String key
) {
184 Node
<V
> node
= mMap
.get(key
);
186 return node
.getPayload();
194 * Builds a key to index files
196 * @param account Account where the file to download is stored
197 * @param remotePath Path of the file in the server
199 private String
buildKey(Account account
, String remotePath
) {
200 return account
.name
+ remotePath
;