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

evolvedbinary / elemental / 982

29 Apr 2025 08:34PM UTC coverage: 56.409% (+0.007%) from 56.402%
982

push

circleci

adamretter
[feature] Improve README.md badges

28451 of 55847 branches covered (50.94%)

Branch coverage included in aggregate %.

77468 of 131924 relevant lines covered (58.72%)

0.59 hits per line

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

77.42
/exist-core/src/main/java/org/exist/collections/ManagedLocks.java
1
/*
2
 * Elemental
3
 * Copyright (C) 2024, Evolved Binary Ltd
4
 *
5
 * admin@evolvedbinary.com
6
 * https://www.evolvedbinary.com | https://www.elemental.xyz
7
 *
8
 * Use of this software is governed by the Business Source License 1.1
9
 * included in the LICENSE file and at www.mariadb.com/bsl11.
10
 *
11
 * Change Date: 2028-04-27
12
 *
13
 * On the date above, in accordance with the Business Source License, use
14
 * of this software will be governed by the Apache License, Version 2.0.
15
 *
16
 * Additional Use Grant: Production use of the Licensed Work for a permitted
17
 * purpose. A Permitted Purpose is any purpose other than a Competing Use.
18
 * A Competing Use means making the Software available to others in a commercial
19
 * product or service that: substitutes for the Software; substitutes for any
20
 * other product or service we offer using the Software that exists as of the
21
 * date we make the Software available; or offers the same or substantially
22
 * similar functionality as the Software.
23
 */
24
package org.exist.collections;
25

26
import org.apache.logging.log4j.LogManager;
27
import org.apache.logging.log4j.Logger;
28
import org.exist.storage.lock.ManagedLock;
29

30
import java.util.Arrays;
31
import java.util.Iterator;
32
import java.util.List;
33

34
/**
35
 * Simple container for a List of ManagedLocks
36
 * which allows ARM (Automatic Resource Management)
37
 * via {@link AutoCloseable}
38
 *
39
 * Locks will be released in the reverse order to which they
40
 * are provided
41
 */
42
public class ManagedLocks<T extends ManagedLock> implements Iterable<T>, AutoCloseable {
43

44
    private final static Logger LOG = LogManager.getLogger(ManagedLocks.class);
1✔
45

46
    private final List<T> managedLocks;
47

48
    /**
49
     * @param managedLocks A list of ManagedLocks which should
50
     *   be in the same order that they were acquired
51
     */
52
    public ManagedLocks(final java.util.List<T> managedLocks) {
1✔
53
        this.managedLocks = managedLocks;
1✔
54
    }
1✔
55

56
    /**
57
     * @param managedLocks An array / var-args of ManagedLocks
58
     *   which should be in the same order that they were acquired
59
     */
60
    public ManagedLocks(final T... managedLocks) {
1✔
61
        this.managedLocks = Arrays.asList(managedLocks);
1✔
62
    }
1✔
63

64
    @Override
65
    public Iterator<T> iterator() {
66
        return new ManagedLockIterator();
1✔
67
    }
68

69
    private class ManagedLockIterator implements Iterator<T> {
1✔
70
        private final Iterator<T> iterator = managedLocks.iterator();
1✔
71

72
        @Override
73
        public boolean hasNext() {
74
            return iterator.hasNext();
1✔
75
        }
76

77
        @Override
78
        public T next() {
79
            return iterator.next();
1✔
80
        }
81
    }
82

83
    @Override
84
    public void close() {
85
        closeAll(managedLocks);
1✔
86
    }
1✔
87

88
    /**
89
     * Closes all the locks in the provided list.
90
     *
91
     * Locks will be closed in reverse (acquisition) order.
92
     *
93
     * If a {@link RuntimeException} occurs when closing
94
     * any lock. The first exception will be recorded and
95
     * lock closing will continue. After all locks are closed
96
     * the first encountered exception is rethrown.
97
     *
98
     * @param <T> The type of the ManagedLocks
99
     * @param managedLocks A list of locks, the list should be ordered in lock acquisition order.
100
     */
101
    public static <T extends ManagedLock> void closeAll(final List<T> managedLocks) {
102
        RuntimeException firstException = null;
1✔
103

104
        for(int i = managedLocks.size() - 1; i >= 0; i--) {
1✔
105
            final T managedLock = managedLocks.get(i);
1✔
106
            try {
107
                managedLock.close();
1✔
108
            } catch (final RuntimeException e) {
1✔
109
                LOG.error(e);
×
110
                if(firstException == null) {
×
111
                    firstException = e;
×
112
                }
113
            }
114
        }
115

116
        if(firstException != null) {
1!
117
            throw firstException;
×
118
        }
119
    }
1✔
120
}
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