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

link-intersystems / lis-commons / #228

03 Sep 2023 10:11AM UTC coverage: 89.823% (-0.2%) from 89.982%
#228

Pull #4

renelink
Added java.xml lib to coveralls plugin.
Pull Request #4: Feature/java 11 migration

31 of 31 new or added lines in 5 files covered. (100.0%)

7379 of 8215 relevant lines covered (89.82%)

0.9 hits per line

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

27.78
/lis-commons-lang/src/main/java/com/link_intersystems/lang/ref/CopyOnAccessReference.java
1
/**
2
 * Copyright 2011 Link Intersystems GmbH <rene.link@link-intersystems.com>
3
 * <p>
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 * <p>
8
 * http://www.apache.org/licenses/LICENSE-2.0
9
 * <p>
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package com.link_intersystems.lang.ref;
17

18
import java.io.*;
19

20

21
/**
22
 * A {@link Reference} that copies the referent on every {@link #get()} call by
23
 * serialization/deserialization.
24
 *
25
 * @param <T>
26
 * @author René Link <a
27
 * href="mailto:rene.link@link-intersystems.com">[rene.link@link-
28
 * intersystems.com]</a>
29
 * @since 1.2.0;
30
 */
31
public class CopyOnAccessReference<T extends Serializable> implements
32
        Reference<T> {
33

34
    private final T referentToCopyOnAccess;
35

36
    /**
37
     * Constructs a new {@link CopyOnAccessReference} that copies the referent
38
     * on every access ({@link #get()} invocation).
39
     *
40
     * @param referentToCopyOnAccess the template referent.
41
     * @since 1.2.0;
42
     */
43
    public CopyOnAccessReference(T referentToCopyOnAccess) {
1✔
44
        this.referentToCopyOnAccess = referentToCopyOnAccess;
1✔
45
    }
1✔
46

47
    /**
48
     * {@inheritDoc}. Always returns a copy of the referent.
49
     *
50
     * @since 1.2.0;
51
     */
52
    public T get() {
53
        if (referentToCopyOnAccess == null) {
1✔
54
            return null;
1✔
55
        }
56
        T clone = null;
×
57
        try {
58
            clone = clone(referentToCopyOnAccess);
×
59
        } catch (IOException | ClassNotFoundException e) {
×
60
            throw new IllegalStateException("Unable to clone reference.", e);
×
61
        }
×
62
        return clone;
×
63
    }
64

65
    public <T extends Serializable> byte[] serialize(T object) throws IOException {
66
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
×
67

68
        try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(bout)) {
×
69
            objectOutputStream.writeObject(object);
×
70
        }
71

72
        return bout.toByteArray();
×
73
    }
74

75
    public <T> T deserialize(byte[] serializedObject) throws IOException, ClassNotFoundException {
76
        try (ObjectInputStream objIn = new ObjectInputStream(new ByteArrayInputStream(serializedObject))) {
×
77
            return (T) objIn.readObject();
×
78
        }
79
    }
80

81
    public <T extends Serializable> T clone(T serializable) throws IOException, ClassNotFoundException {
82
        return deserialize(serialize(serializable));
×
83
    }
84
}
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