Skip to content

Commit

Permalink
Support abstract classes declaring static interned instances of their…
Browse files Browse the repository at this point in the history
… own type
  • Loading branch information
Stephan202 committed Mar 18, 2021
1 parent f7dd7ec commit 9cd6cc7
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Copyright 2021 Immutables Authors and Contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.immutables.fixture;

import org.immutables.value.Value;

@Value.Immutable(intern = true)
@Value.Style(allParameters = true)
public abstract class InternedClassWithSelfConstant {
public static final ImmutableInternedClassWithSelfConstant FOO = ImmutableInternedClassWithSelfConstant.of("foo");

abstract String getValue();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Copyright 2021 Immutables Authors and Contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.immutables.fixture;

import org.immutables.value.Value;

@Value.Immutable(intern = true)
@Value.Style(allParameters = true)
interface InternedInterfaceWithSelfConstant {
ImmutableInternedInterfaceWithSelfConstant FOO = ImmutableInternedInterfaceWithSelfConstant.of("foo");

String getValue();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Copyright 2021 Immutables Authors and Contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.immutables.fixture;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertSame;

public class InternedClassWithSelfConstantTest {
@Test
public void testInstantiation() {
assertSame(ImmutableInternedClassWithSelfConstant.of("foo"), ImmutableInternedClassWithSelfConstant.FOO);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Copyright 2021 Immutables Authors and Contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.immutables.fixture;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertSame;

public class InternedInterfaceWithSelfConstantTest {
@Test
public void testInstantiation() {
assertSame(ImmutableInternedInterfaceWithSelfConstant.of("foo"), ImmutableInternedInterfaceWithSelfConstant.FOO);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -697,21 +697,23 @@ public [type.typeAbstract.relative] [v.names.with]([v.atNullability][v.type] val
[else if type.useSingletonOnly][-- deliberately left empty --]
[else if type.useInterned]

private static final class InternerHolder {
[if type.generateJdkOnly]
[if type.useWeakInterned]
private static final java.util.Map<[type.typeImmutable.simple], java.lang.ref.WeakReference<[type.typeImmutable.simple]>> INTERNER =
new java.util.WeakHashMap<>();
private static final java.util.Map<[type.typeImmutable.simple], java.lang.ref.WeakReference<[type.typeImmutable.simple]>> INTERNER =
new java.util.WeakHashMap<>();
[else]
private static final java.util.concurrent.ConcurrentHashMap<InternProxy, [type.typeImmutable.relative]> INTERNER =
private static final java.util.concurrent.ConcurrentHashMap<InternProxy, [type.typeImmutable.relative]> INTERNER =
new java.util.concurrent.ConcurrentHashMap<InternProxy, [type.typeImmutable.relative]>();
[/if]
[else]
[if type.useWeakInterned]
private static final [guava].collect.Interner<[type.typeImmutable.relative]> INTERNER = [guava].collect.Interners.newWeakInterner();
private static final [guava].collect.Interner<[type.typeImmutable.relative]> INTERNER = [guava].collect.Interners.newWeakInterner();
[else]
private static final [guava].collect.Interner<InternProxy> INTERNER = [guava].collect.Interners.newStrongInterner();
private static final [guava].collect.Interner<InternProxy> INTERNER = [guava].collect.Interners.newStrongInterner();
[/if]
[/if]
}
[/if]
[/template]

Expand Down Expand Up @@ -785,24 +787,24 @@ public [type.typeAbstract.relative] [v.names.with]([v.atNullability][v.type] val
[else if type.useInterned]
[if type.generateJdkOnly]
[if type.useWeakInterned]
synchronized (INTERNER) {
[atNullable type]java.lang.ref.WeakReference<[type.typeImmutable.relative]> reference = INTERNER.get(instance);
synchronized (InternerHolder.INTERNER) {
[atNullable type]java.lang.ref.WeakReference<[type.typeImmutable.relative]> reference = InternerHolder.INTERNER.get(instance);
[atNullable type][type.typeImmutable.relative] interned = reference != null ? reference.get() : null;
if (interned == null) {
INTERNER.put(instance, new java.lang.ref.WeakReference<>(instance));
InternerHolder.INTERNER.put(instance, new java.lang.ref.WeakReference<>(instance));
interned = instance;
}
return interned;
}
[else]
[atNullable type][type.typeImmutable.relative] interned = INTERNER.putIfAbsent(new InternProxy(instance), instance);
[atNullable type][type.typeImmutable.relative] interned = InternerHolder.INTERNER.putIfAbsent(new InternProxy(instance), instance);
return interned != null ? interned : instance;
[/if]
[else]
[if type.useWeakInterned]
return INTERNER.intern(instance);
return InternerHolder.INTERNER.intern(instance);
[else]
return INTERNER.intern(new InternProxy(instance)).instance;
return InternerHolder.INTERNER.intern(new InternProxy(instance)).instance;
[/if]
[/if]
[else if type.useSingleton]
Expand Down

0 comments on commit 9cd6cc7

Please sign in to comment.