Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
.gradle
build/
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
language: java

jdk:
- oraclejdk8
Empty file removed README.md
Empty file.
15 changes: 15 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
group 'spbau.shevchenko'
version '1.0-SNAPSHOT'

apply plugin: 'java'

//noinspection GroovyUnusedAssignment
sourceCompatibility = 1.8

repositories {
mavenCentral()
}

dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
}
Binary file added gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
6 changes: 6 additions & 0 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#Thu Mar 02 20:22:38 MSK 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-all.zip
164 changes: 164 additions & 0 deletions gradlew

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

90 changes: 90 additions & 0 deletions gradlew.bat

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rootProject.name = 'Lazy'

17 changes: 17 additions & 0 deletions src/main/java/ru/spbau/shevchenko/Lazy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ru.spbau.shevchenko;

/**
* An object that holds a result of lazy computation.
*
* It should be the same object on all calls of {@link Lazy#get() ru.spbau.shevchenko.Lazy::get()} method.
*
* @param <T> type of a result of computation
*/
@SuppressWarnings("WeakerAccess")
public interface Lazy<T> {
/**
* Method that performs computation.
* @return result of computation
*/
T get();
}
44 changes: 44 additions & 0 deletions src/main/java/ru/spbau/shevchenko/LazyFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package ru.spbau.shevchenko;

import java.util.function.Supplier;

/**
* Class ru.spbau.shevchenko.LazyFactory provides factory methods for creating instances of different {@link Lazy} implementations.
*/
public class LazyFactory {
private LazyFactory(){}

/**
* Returns {@link Lazy} object suitable for use in a single thread.
* It's guaranteed, that computation will be done only once.
* @param supplier computation to perform
* @param <T> type of computation result
* @return result of computation
*/
public static <T> Lazy<T> createSingleThreadedLazy(Supplier<T> supplier){
return new SingleThreadedLazy<>(supplier);
}

/**
* Returns {@link Lazy} object suitable for use in multiple threads.
* It's guaranteed, that computation will be done only once.
* @param supplier computation to perform.
* @param <T> type of computation result
* @return result of computation
*/
public static <T> Lazy<T> createMultiThreadedLazy(Supplier<T> supplier){
return new MultiThreadedLazy<>(supplier);
}

/**
* Returns {@link Lazy} object suitable for use in multiple threads.
* No locks on object will be done.
* Computation may be performed several times but resulting object will always be the same
* @param supplier computation to perform.
* @param <T> type of computation result
* @return result of computation
*/
public static <T> Lazy<T> createLockFreeLazy(Supplier<T> supplier){
return new LockFreeLazy<>(supplier);
}
}
42 changes: 42 additions & 0 deletions src/main/java/ru/spbau/shevchenko/LockFreeLazy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package ru.spbau.shevchenko;

import javax.xml.ws.Holder;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import java.util.function.Supplier;

/**
* Lock-free implementation of {@link Lazy} interface suitable when you need to call {@link #get()} in multiple threads.
* @param <T> type of a result of computation
*/
@SuppressWarnings("WeakerAccess")
public class LockFreeLazy<T> implements Lazy<T> {
private Supplier<T> supplier = null;
private volatile Holder<T> result = null;

@SuppressWarnings("AtomicFieldUpdaterIssues")
private static final AtomicReferenceFieldUpdater<LockFreeLazy, Holder> resultUpdater =
AtomicReferenceFieldUpdater.newUpdater(LockFreeLazy.class, Holder.class, "result");

/**
* Creates holder for a lazy computation provided by a {@link Supplier} object.
* @param supplier computation provider
*/
public LockFreeLazy(Supplier<T> supplier) {
this.supplier = supplier;
}


/**
* Returns result of a computation.
* Computation may be performed several times, but this method will always return the same object.
* @return result of a computations
*/
@Override
public T get() {
if (result == null) {
final Holder<T> value = new Holder<>(supplier.get());
resultUpdater.compareAndSet(this, null, value);
}
return result.value;
}
}
Loading