-
Notifications
You must be signed in to change notification settings - Fork 0
Hw1.lazy #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ilkivoo
wants to merge
4
commits into
master
Choose a base branch
from
hw1.Lazy
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Hw1.lazy #1
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| language: java | ||
| jdk: | ||
| - oraclejdk8 | ||
| os: | ||
| - linux | ||
| script: | ||
| - chmod +x buildscript.sh && ./buildscript.sh |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <groupId>01</groupId> | ||
| <artifactId>Lazy</artifactId> | ||
| <version>1.0-SNAPSHOT</version> | ||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-compiler-plugin</artifactId> | ||
| <configuration> | ||
| <source>1.8</source> | ||
| <target>1.8</target> | ||
| </configuration> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| <dependencies> | ||
| <dependency> | ||
| <groupId>junit</groupId> | ||
| <artifactId>junit</artifactId> | ||
| <version>4.12</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.jetbrains</groupId> | ||
| <artifactId>annotations</artifactId> | ||
| <version>15.0</version> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
|
|
||
| </project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package ru.spbau.mit.alyokhina; | ||
| /** | ||
| * Lazy computing interface | ||
| * @param <T> type of returned object | ||
| */ | ||
| public interface Lazy<T> { | ||
| T get(); | ||
| } |
102 changes: 102 additions & 0 deletions
102
01.Lazy/src/main/java/ru/spbau/mit/alyokhina/LazyFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| package ru.spbau.mit.alyokhina; | ||
|
|
||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| import java.util.function.Supplier; | ||
|
|
||
| /** | ||
| * Class for create object of Lazy | ||
| * There is a single-threaded and multithreaded version of the creation | ||
| * | ||
| * @param <T> type of Lazy object | ||
| */ | ||
| public class LazyFactory<T> { | ||
| /** | ||
| * Create Lazy object for single thread | ||
| * | ||
| * @param supplier the Lazy object is created on the basis of calculations (represented by the supplier) | ||
| * @param <T> type of Lazy object | ||
| * @return Lazy object | ||
| */ | ||
| @NotNull | ||
| public static <T> Lazy<T> createLazySingleThreadMode(Supplier<T> supplier) { | ||
| return new LazySingleThreadMode<>(supplier); | ||
| } | ||
|
|
||
| /** | ||
| * Create Lazy object for many threads | ||
| * | ||
| * @param supplier the Lazy object is created on the basis of calculations (represented by the supplier) | ||
| * @param <T> type of Lazy object | ||
| * @return Lazy object | ||
| */ | ||
| @NotNull | ||
| public static <T> Lazy<T> createLazyMultiThreadedMode(Supplier<T> supplier) { | ||
| return new LazyMultiThreadMode<>(supplier); | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * class implementing interface Lazy for one thread | ||
| * | ||
| * @param <T> type of Lazy object | ||
| */ | ||
| private static class LazySingleThreadMode<T> implements Lazy<T> { | ||
| /** The Lazy object is created on the basis of calculations (represented by the supplier) */ | ||
| private Supplier<T> supplier; | ||
|
|
||
| /** Value that came after calling the supplier (null - if there was no call) */ | ||
| private T ans; | ||
|
|
||
| /** Constructor */ | ||
| private LazySingleThreadMode(Supplier<T> supplier) { | ||
| this.supplier = supplier; | ||
| } | ||
|
|
||
| /** | ||
| * If the value has not yet been calculated, then calculate | ||
| * | ||
| * @return the value that was received | ||
| */ | ||
| @Override | ||
| public T get() { | ||
| if (ans == null) { | ||
| ans = supplier.get(); | ||
| } | ||
| return ans; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * class implementing interface Lazy for many threads | ||
| * | ||
| * @param <T> type of Lazy object | ||
| */ | ||
| private static class LazyMultiThreadMode<T> implements Lazy<T> { | ||
| /** The Lazy object is created on the basis of calculations (represented by the supplier) */ | ||
| private volatile Supplier<T> supplier; | ||
|
|
||
| /** Value that came after calling the supplier (null - if there was no call) */ | ||
| private T ans; | ||
|
|
||
| /** Constructor */ | ||
| private LazyMultiThreadMode(Supplier<T> supplier) { | ||
| this.supplier = supplier; | ||
| } | ||
|
|
||
| /** | ||
| * If the value has not yet been calculated, then calculate | ||
| * | ||
| * @return the value that was received | ||
| */ | ||
| @Override | ||
| public T get() { | ||
| synchronized (this) { | ||
| if (ans == null) { | ||
| ans = supplier.get(); | ||
| } | ||
| } | ||
| return ans; | ||
| } | ||
| } | ||
| } | ||
79 changes: 79 additions & 0 deletions
79
01.Lazy/src/test/java/ru/spbau/mit/alyokhina/LazyFactoryTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package ru.spbau.mit.alyokhina; | ||
|
|
||
| import org.junit.Test; | ||
| import java.util.function.Supplier; | ||
| import static org.junit.Assert.*; | ||
|
|
||
| public class LazyFactoryTest { | ||
| @Test | ||
| public void testCreateLazySingleThreadMode() { | ||
| Lazy<Integer> test = LazyFactory.createLazySingleThreadMode(() -> 5); | ||
| assertEquals((Integer) 5, test.get()); | ||
| assertEquals((Integer) 5, test.get()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCreateLazySingleThreadModeIfSupplierGetNull() { | ||
| Lazy<Integer> test = LazyFactory.createLazySingleThreadMode(() -> null); | ||
| assertEquals(null, test.get()); | ||
| assertEquals(null, test.get()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCreateLazySingleThreadModeIfSupplierChangeValue() { | ||
| Lazy<Integer> test = LazyFactory.createLazySingleThreadMode(new Supplier<Integer>() { | ||
| private boolean flag = false; | ||
|
|
||
| @Override | ||
| public Integer get() { | ||
| Integer ans = flag ? 5 : 6; | ||
| flag = true; | ||
| return ans; | ||
| } | ||
| }); | ||
| assertEquals((Integer) 6, test.get()); | ||
| assertEquals((Integer) 6, test.get()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCreateLazyMultiThreadedModeForOneThread() { | ||
| Lazy<Integer> test = LazyFactory.createLazyMultiThreadedMode(() -> 5); | ||
| assertEquals((Integer) 5, test.get()); | ||
| assertEquals((Integer) 5, test.get()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCreateLazyMultiThreadedModeForThreads() { | ||
| Lazy<Integer> test = LazyFactory.createLazyMultiThreadedMode(() -> 5); | ||
| Thread[] threads = new Thread[1000]; | ||
| for (int i = 0; i < 1000; i++) { | ||
| threads[i] = new Thread(() -> assertEquals((Integer) 5, test.get())); | ||
| } | ||
| for (Thread thread : threads) { | ||
| thread.run(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testCreateLazyMultiThreadedModeForThreadsIfSupplierChange() throws Exception { | ||
| Lazy<Integer> test = LazyFactory.createLazyMultiThreadedMode(new Supplier<Integer>() { | ||
| private boolean flag = false; | ||
|
|
||
| @Override | ||
| public Integer get() { | ||
| Integer ans = flag ? 5 : 6; | ||
| flag = true; | ||
| return ans; | ||
| } | ||
| }); | ||
| Thread[] threads = new Thread[1000]; | ||
| for (int i = 0; i < 1000; i++) { | ||
|
|
||
| threads[i] = new Thread(() -> assertEquals((Integer) 6, test.get())); | ||
| } | ||
| for (Thread thread : threads) { | ||
| thread.run(); | ||
| } | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| #!/bin/bash | ||
| files=$(find . -maxdepth 1 -type d | grep "./0.*") | ||
| for file in $files | ||
| do | ||
| cd $file | ||
| mvn test -B | ||
| cd ../ | ||
| done | ||
|
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
а можно как-то засинхронизировать только код, который будет инициализировать значение?