GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Builder   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
rs 10
eloc 12
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A Builder() 0 3 1
A build() 0 2 1
A addRestoreProperty(String,String) 0 2 1
A addPropertyName(String) 0 2 1
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 * contributor license agreements.  See the NOTICE file distributed with
4
 * this work for additional information regarding copyright ownership.
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 * (the "License"); you may not use this file except in compliance with
7
 * the License.  You may obtain a copy of the License at
8
 *
9
 *      http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
package io.github.glytching.junit.extension.system;
18
19
import java.util.HashMap;
20
import java.util.HashSet;
21
import java.util.Map;
22
import java.util.Set;
23
24
/**
25
 * A context object which encapsulates what the system property extension did. This allows us to
26
 * reverse any changes made by the extension after test execution completes. For example:
27
 *
28
 * <ul>
29
 *   <li>If a new system property was added then we remove it
30
 *   <li>If an existing system property was overwritten then we reinstate its original value
31
 * </ul>
32
 */
33
final class RestoreContext {
34
  private final Set<String> propertyNames;
35
  private final Map<String, String> restoreProperties;
36
37
  /**
38
   * Created using the {@link Builder}.
39
   *
40
   * @param propertyNames
41
   * @param restoreProperties
42
   */
43
  private RestoreContext(Set<String> propertyNames, Map<String, String> restoreProperties) {
44
    this.propertyNames = new HashSet<>(propertyNames);
45
    this.restoreProperties = new HashMap<>(restoreProperties);
46
  }
47
48
  public static Builder createBuilder() {
49
    return new Builder();
50
  }
51
52
  /**
53
   * Reverse the system property 'sets' performed on behalf of this restore context.
54
   *
55
   * <p>For each entry in {@link #propertyNames}, if {@link #restoreProperties} contains an entry
56
   * then reset the system property with the value from {@link #restoreProperties} otherwise just
57
   * remove the system property for that property name.
58
   */
59
  public void restore() {
60
    for (String propertyName : propertyNames) {
61
      if (restoreProperties.containsKey(propertyName)) {
62
        // reinstate the original value
63
        System.setProperty(propertyName, restoreProperties.get(propertyName));
64
      } else {
65
        // remove the (previously unset) property
66
        System.clearProperty(propertyName);
67
      }
68
    }
69
  }
70
71
  /**
72
   * Simple builder implementation allowing a {@link RestoreContext} to be built up as we walk
73
   * through system property configuration.
74
   */
75
  final static class Builder {
76
    private final Set<String> properties;
77
    private final Map<String, String> restoreProperties;
78
79
    private Builder() {
80
      properties = new HashSet<>();
81
      restoreProperties = new HashMap<>();
82
    }
83
84
    void addPropertyName(String propertyName) {
85
      properties.add(propertyName);
86
    }
87
88
    void addRestoreProperty(String propertyName, String propertyValue) {
89
      restoreProperties.put(propertyName, propertyValue);
90
    }
91
92
    RestoreContext build() {
93
      return new RestoreContext(properties, restoreProperties);
94
    }
95
  }
96
}
97