• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

devonfw / IDEasy / 13858963404

14 Mar 2025 02:38PM UTC coverage: 67.657% (-1.0%) from 68.619%
13858963404

push

github

web-flow
#1024: Move urls into url-updater module (#1025)

Co-authored-by: Jörg Hohwiller <hohwille@users.noreply.github.com>

3036 of 4915 branches covered (61.77%)

Branch coverage included in aggregate %.

7825 of 11138 relevant lines covered (70.25%)

3.07 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

46.25
cli/src/main/java/com/devonfw/tools/ide/url/model/folder/AbstractUrlFolder.java
1
package com.devonfw.tools.ide.url.model.folder;
2

3
import java.io.IOException;
4
import java.nio.file.Files;
5
import java.nio.file.Path;
6
import java.util.Collection;
7
import java.util.Collections;
8
import java.util.HashMap;
9
import java.util.Iterator;
10
import java.util.Map;
11
import java.util.Set;
12
import java.util.stream.Stream;
13

14
import org.slf4j.Logger;
15
import org.slf4j.LoggerFactory;
16

17
import com.devonfw.tools.ide.url.model.AbstractUrlArtifact;
18
import com.devonfw.tools.ide.url.model.UrlArtifactWithParent;
19

20
/**
21
 * Class from which UrlRepository inherits, as its objects don't have a parent, but possibly child objects of the class UrlTool.
22
 *
23
 * @param <C> Type of the child object.
24
 */
25
public abstract class AbstractUrlFolder<C extends UrlArtifactWithParent<?>> extends AbstractUrlArtifact
26
    implements UrlFolder<C> {
27

28
  private static final Logger LOG = LoggerFactory.getLogger(AbstractUrlFolder.class);
4✔
29

30
  private final Map<String, C> childMap;
31

32
  private final Set<String> childNames;
33

34
  private final Collection<C> children;
35

36
  /**
37
   * The constructor.
38
   *
39
   * @param path the {@link #getPath() path}.
40
   * @param name the {@link #getName() filename}.
41
   */
42
  public AbstractUrlFolder(Path path, String name) {
43

44
    super(path, name);
4✔
45
    this.childMap = new HashMap<>();
5✔
46
    this.childNames = Collections.unmodifiableSet(this.childMap.keySet());
6✔
47
    this.children = Collections.unmodifiableCollection(this.childMap.values());
6✔
48
  }
1✔
49

50
  @Override
51
  public int getChildCount() {
52

53
    load(false);
×
54
    return this.childMap.size();
×
55
  }
56

57
  @Override
58
  public C getChild(String name) {
59

60
    load(false);
3✔
61
    return this.childMap.get(name);
6✔
62
  }
63

64
  @Override
65
  public C getOrCreateChild(String name) {
66

67
    return this.childMap.computeIfAbsent(name, p -> newChild(name));
13✔
68
  }
69

70
  @Override
71
  public void deleteChild(String name) {
72

73
    C child = this.childMap.remove(name);
×
74
    if (child != null) {
×
75
      delete(child.getPath());
×
76
    }
77
  }
×
78

79
  private static void delete(Path path) {
80

81
    LOG.debug("Deleting {}", path);
×
82
    if (Files.exists(path)) {
×
83
      try {
84
        deleteRecursive(path);
×
85
      } catch (IOException e) {
×
86
        throw new RuntimeException("Failed to delete " + path);
×
87
      }
×
88
    } else {
89
      LOG.warn("Could not delete file {} because it does not exist.", path);
×
90
    }
91
  }
×
92

93
  private static void deleteRecursive(Path path) throws IOException {
94

95
    if (Files.isDirectory(path)) {
×
96
      try (Stream<Path> childStream = Files.list(path)) {
×
97
        Iterator<Path> iterator = childStream.iterator();
×
98
        while (iterator.hasNext()) {
×
99
          Path child = iterator.next();
×
100
          deleteRecursive(child);
×
101
        }
×
102
      }
103
    }
104
    LOG.trace("Deleting {}", path);
×
105
    Files.delete(path);
×
106

107
  }
×
108

109
  @Override
110
  public Collection<C> getChildren() {
111

112
    load(false);
3✔
113
    return this.children;
3✔
114
  }
115

116
  /**
117
   * @param name the plain filename (excluding any path).
118
   * @param folder - {@code true} in case of a folder, {@code false} otherwise (plain data file).
119
   * @return {@code true} if the existing file from the file-system should be {@link #getOrCreateChild(String) created as child}, {@code false} otherwise
120
   *     (ignore the file).
121
   */
122
  protected boolean isAllowedChild(String name, boolean folder) {
123

124
    return folder;
2✔
125
  }
126

127
  /**
128
   * @return the {@link Set} with all {@link #getName() names} of the children.
129
   */
130
  public Set<String> getChildNames() {
131

132
    return this.childNames;
×
133
  }
134

135
  @Override
136
  public void load(boolean recursive) {
137

138
    if (!this.loaded) {
3✔
139
      Path path = getPath();
3✔
140
      if (Files.isDirectory(path)) {
5✔
141
        try (Stream<Path> childStream = Files.list(path)) {
3✔
142
          childStream.forEach(c -> loadChild(c, recursive));
10✔
143
        } catch (IOException e) {
×
144
          throw new IllegalStateException("Failed to list children of directory " + path, e);
×
145
        }
1✔
146
      }
147
      this.loaded = true;
3✔
148
    }
149
  }
1✔
150

151
  private void loadChild(Path childPath, boolean recursive) {
152

153
    String name = childPath.getFileName().toString();
4✔
154
    if (name.startsWith(".")) {
4!
155
      return; // ignore hidden files and folders (e.g. ".git")
×
156
    }
157
    boolean folder = Files.isDirectory(childPath);
5✔
158
    if (isAllowedChild(name, folder)) {
5✔
159
      C child = getOrCreateChild(name);
4✔
160
      if (recursive) {
2✔
161
        child.load(recursive);
3✔
162
      }
163
    }
164
  }
1✔
165

166
  /**
167
   * @param name the {@link #getName() name} of the requested child.
168
   * @return the new child object.
169
   */
170
  protected abstract C newChild(String name);
171

172
  @Override
173
  public void save() {
174

175
    for (C child : this.childMap.values()) {
×
176
      child.save();
×
177
    }
×
178
  }
×
179
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc