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

devonfw / IDEasy / 9907372175

12 Jul 2024 11:49AM UTC coverage: 61.142% (-0.02%) from 61.162%
9907372175

push

github

hohwille
fixed tests

1997 of 3595 branches covered (55.55%)

Branch coverage included in aggregate %.

5296 of 8333 relevant lines covered (63.55%)

2.8 hits per line

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

91.84
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.Map;
10
import java.util.Set;
11
import java.util.stream.Stream;
12

13
import com.devonfw.tools.ide.url.model.AbstractUrlArtifact;
14
import com.devonfw.tools.ide.url.model.UrlArtifactWithParent;
15

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

24
  private final Map<String, C> childMap;
25

26
  private final Set<String> childNames;
27

28
  private final Collection<C> children;
29

30
  /**
31
   * The constructor.
32
   *
33
   * @param path the {@link #getPath() path}.
34
   * @param name the {@link #getName() filename}.
35
   */
36
  public AbstractUrlFolder(Path path, String name) {
37

38
    super(path, name);
4✔
39
    this.childMap = new HashMap<>();
5✔
40
    this.childNames = Collections.unmodifiableSet(this.childMap.keySet());
6✔
41
    this.children = Collections.unmodifiableCollection(this.childMap.values());
6✔
42
  }
1✔
43

44
  @Override
45
  public int getChildCount() {
46

47
    load(false);
3✔
48
    return this.childMap.size();
4✔
49
  }
50

51
  @Override
52
  public C getChild(String name) {
53

54
    load(false);
3✔
55
    return this.childMap.get(name);
6✔
56
  }
57

58
  @Override
59
  public C getOrCreateChild(String name) {
60

61
    return this.childMap.computeIfAbsent(name, p -> newChild(name));
13✔
62
  }
63

64
  @Override
65
  public Collection<C> getChildren() {
66

67
    load(false);
3✔
68
    return this.children;
3✔
69
  }
70

71
  /**
72
   * @param name the plain filename (excluding any path).
73
   * @param folder - {@code true} in case of a folder, {@code false} otherwise (plain data file).
74
   * @return {@code true} if the existing file from the file-system should be {@link #getOrCreateChild(String) created as child}, {@code false} otherwise
75
   * (ignore the file).
76
   */
77
  protected boolean isAllowedChild(String name, boolean folder) {
78

79
    return folder;
2✔
80
  }
81

82
  /**
83
   * @return the {@link Set} with all {@link #getName() names} of the children.
84
   */
85
  public Set<String> getChildNames() {
86

87
    return this.childNames;
3✔
88
  }
89

90
  @Override
91
  public void load(boolean recursive) {
92

93
    if (!this.loaded) {
3✔
94
      Path path = getPath();
3✔
95
      if (Files.isDirectory(path)) {
5✔
96
        try (Stream<Path> childStream = Files.list(path)) {
3✔
97
          childStream.forEach(c -> loadChild(c, recursive));
10✔
98
        } catch (IOException e) {
×
99
          throw new IllegalStateException("Failed to list children of directory " + path, e);
×
100
        }
1✔
101
      }
102
      this.loaded = true;
3✔
103
    }
104
  }
1✔
105

106
  private void loadChild(Path childPath, boolean recursive) {
107

108
    String name = childPath.getFileName().toString();
4✔
109
    if (name.startsWith(".")) {
4!
110
      return; // ignore hidden files and folders (e.g. ".git")
×
111
    }
112
    boolean folder = Files.isDirectory(childPath);
5✔
113
    if (isAllowedChild(name, folder)) {
5✔
114
      C child = getOrCreateChild(name);
4✔
115
      if (recursive) {
2✔
116
        child.load(recursive);
3✔
117
      }
118
    }
119
  }
1✔
120

121
  /**
122
   * @param name the {@link #getName() name} of the requested child.
123
   * @return the new child object.
124
   */
125
  protected abstract C newChild(String name);
126

127
  @Override
128
  public void save() {
129

130
    for (C child : this.childMap.values()) {
12✔
131
      ((AbstractUrlArtifact) child).save();
3✔
132
    }
1✔
133
  }
1✔
134
}
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

© 2026 Coveralls, Inc