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
);
93 public void clearPayload() {
99 public /* synchronized */ Pair
<String
, String
> putIfAbsent(Account account
, String remotePath
, V value
) {
100 String targetKey
= buildKey(account
, remotePath
);
101 Node
<V
> valuedNode
= new Node(targetKey
, value
);
107 String currentPath
= remotePath
, parentPath
= null
, parentKey
= null
;
108 Node
<V
> currentNode
= valuedNode
, parentNode
= null
;
109 boolean linked
= false
;
110 while (!OCFile
.ROOT_PATH
.equals(currentPath
) && !linked
) {
111 parentPath
= new File(currentPath
).getParent();
112 if (!parentPath
.endsWith(OCFile
.PATH_SEPARATOR
)) {
113 parentPath
+= OCFile
.PATH_SEPARATOR
;
115 parentKey
= buildKey(account
, parentPath
);
116 parentNode
= mMap
.get(parentKey
);
117 if (parentNode
== null
) {
118 parentNode
= new Node(parentKey
, null
);
119 parentNode
.addChild(currentNode
);
120 mMap
.put(parentKey
, parentNode
);
122 parentNode
.addChild(currentNode
);
125 currentPath
= parentPath
;
126 currentNode
= parentNode
;
129 String linkedTo
= OCFile
.ROOT_PATH
;
131 linkedTo
= parentNode
.getKey().substring(account
.name
.length());
133 return new Pair
<String
, String
>(targetKey
, linkedTo
);
137 public Pair
<V
, String
> removePayload(Account account
, String remotePath
) {
138 String targetKey
= buildKey(account
, remotePath
);
139 Node
<V
> target
= mMap
.get(targetKey
);
140 if (target
!= null
) {
141 target
.clearPayload();
142 if (!target
.hasChildren()) {
143 return remove(account
, remotePath
);
146 return new Pair
<V
, String
>(null
, null
);
150 public /* synchronized */ Pair
<V
, String
> remove(Account account
, String remotePath
) {
151 String targetKey
= buildKey(account
, remotePath
);
152 Node
<V
> firstRemoved
= mMap
.remove(targetKey
);
153 String unlinkedFrom
= null
;
155 if (firstRemoved
!= null
) {
157 removeDescendants(firstRemoved
);
159 /// remove ancestors if only here due to firstRemoved
160 Node
<V
> removed
= firstRemoved
;
161 Node
<V
> parent
= removed
.getParent();
162 boolean unlinked
= false
;
163 while (parent
!= null
) {
164 parent
.removeChild(removed
);
165 if (!parent
.hasChildren()) {
166 removed
= mMap
.remove(parent
.getKey());
167 parent
= removed
.getParent();
173 if (parent
!= null
) {
174 unlinkedFrom
= parent
.getKey().substring(account
.name
.length());
177 return new Pair
<V
, String
>(firstRemoved
.getPayload(), unlinkedFrom
);
180 return new Pair
<V
, String
>(null
, null
);
183 private void removeDescendants(Node
<V
> removed
) {
184 Iterator
<Node
<V
>> childrenIt
= removed
.getChildren().iterator();
185 Node
<V
> child
= null
;
186 while (childrenIt
.hasNext()) {
187 child
= childrenIt
.next();
188 mMap
.remove(child
.getKey());
189 removeDescendants(child
);
193 public boolean contains(Account account
, String remotePath
) {
194 String targetKey
= buildKey(account
, remotePath
);
195 return mMap
.containsKey(targetKey
);
198 public /* synchronized */ V
get(String key
) {
199 Node
<V
> node
= mMap
.get(key
);
201 return node
.getPayload();
207 public V
get(Account account
, String remotePath
) {
208 String key
= buildKey(account
, remotePath
);
214 * Builds a key to index files
216 * @param account Account where the file to download is stored
217 * @param remotePath Path of the file in the server
219 private String
buildKey(Account account
, String remotePath
) {
220 return account
.name
+ remotePath
;