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
49 changes: 49 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff:
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/dictionaries

# Sensitive or high-churn files:
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.xml
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml

# Gradle:
.idea/**/gradle.xml
.idea/**/libraries

# CMake
cmake-build-debug/

# Mongo Explorer plugin:
.idea/**/mongoSettings.xml

## File-based project format:
*.iws

## Plugin-specific files:

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
16 changes: 16 additions & 0 deletions .idea/compiler.xml

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

13 changes: 13 additions & 0 deletions .idea/misc.xml

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

8 changes: 8 additions & 0 deletions .idea/modules.xml

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

17 changes: 17 additions & 0 deletions Maybe.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: junit:junit:4.8" level="project" />
<orderEntry type="library" name="Maven: org.jetbrains:annotations:13.0" level="project" />
</component>
</module>
36 changes: 36 additions & 0 deletions pom.xml
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>ru.spbau.mit.kazakov.Maybe</groupId>
<artifactId>Maybe</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.8</version>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>13.0</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ru.spbau.mit.kazakov.Maybe;

/**
* Exception thrown when there is no value to return.
*/
public class GetNothingException extends Exception {
}
63 changes: 63 additions & 0 deletions src/main/java/ru/spbau/mit/kazakov/Maybe/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package ru.spbau.mit.kazakov.Maybe;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

/**
* Reads numbers from input file and writes squared values to output file. If read data isn't a number writes "null" instead.
*/
public class Main {
/**
* Primary method.
*
* @param args the first argument is input file and the second one is output file
*/
public static void main(@NotNull String[] args) throws IOException, GetNothingException {
ArrayList<Maybe<Integer>> readData = new ArrayList<>();
try (Scanner scanner = new Scanner(new File(args[0]))) {
while (scanner.hasNextLine()) {
Integer value = tryParse(scanner.nextLine());
if (value == null) {
readData.add(Maybe.nothing());
} else {
readData.add(Maybe.just(value));
}
}
}

ArrayList<Maybe<Integer>> squaredNumber = new ArrayList<>();
for (Maybe<Integer> maybeInt : readData) {
squaredNumber.add(maybeInt.map(value -> value * value));
}

try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(args[1]))) {
for (Maybe<Integer> maybeInt : squaredNumber) {
if (maybeInt.isPresent()) {
bufferedWriter.write(maybeInt.get().toString());
} else {
bufferedWriter.write("null");
}
bufferedWriter.newLine();
}
}
}

/**
* Parses specified String to Integer.
*
* @return Integer if String was a number, and null otherwise
*/
@Nullable
private static Integer tryParse(@NotNull String parsingString) {
try {
return Integer.parseInt(parsingString);
} catch (NumberFormatException e) {
return null;
}
}

}
85 changes: 85 additions & 0 deletions src/main/java/ru/spbau/mit/kazakov/Maybe/Maybe.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package ru.spbau.mit.kazakov.Maybe;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.function.Function;

/**
* Generic container for storing a value or nothing.
*/
public class Maybe<T> {
private static final Maybe NOTHING = new Maybe<>(null);

@Nullable
private final T value;

/**
* Assigns specified value to class's storing field.
*
* @param t specified value
*/
private Maybe(@Nullable T t) {
value = t;
}

/**
* Creates an object for storing specified value.
*
* @param t specified value
* @return new object
*/
@NotNull
public static <T> Maybe<T> just(T t) {
return new Maybe<>(t);
}

/**
* Creates an object for strong nothing.
*
* @return new object
*/
@NotNull
public static <T> Maybe<T> nothing() {
return NOTHING;
}

/**
* Returns stored value or throws exception if there is no value.
*
* @return stored value
* @throws GetNothingException if there is no stored value
*/
@NotNull
public T get() throws GetNothingException {
if (isPresent()) {
return value;
} else {
throw new GetNothingException();
}
}

/**
* Checks if there is stored value.
*
* @return true if there is stored value, and false otherwise
*/
public boolean isPresent() {
return value != null;
}

/**
* Maps stored value of type T to another value of type U.
*
* @param mapper a function acting from T to V
* @param <U> image's type
* @return Maybe object storing mapped value if there is a storing value, and Maybe object storing nothing otherwise
*/
@NotNull
public <U> Maybe<U> map(@NotNull Function<? super T, ? extends U> mapper) {
if (!isPresent()) {
return nothing();
}
return just(mapper.apply(value));
}
}
46 changes: 46 additions & 0 deletions src/test/java/ru/spbau/mit/kazakov/Maybe/MainTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package ru.spbau.mit.kazakov.Maybe;

import org.junit.Test;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

import org.junit.Rule;
import org.junit.rules.TemporaryFolder;

import static org.junit.Assert.*;

public class MainTest {
@Rule
public TemporaryFolder testFolder = new TemporaryFolder();

@Test
public void testMain() throws IOException, GetNothingException {
File input = testFolder.newFile("input.txt");
File output = testFolder.newFile("output.txt");

try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(input))) {
bufferedWriter.write("1\n0\n-3\nj\njava\n1b\n11 22\n");
}

Main.main(new String[]{input.getAbsolutePath(), output.getAbsolutePath()});


assertEquals("1\n0\n9\nnull\nnull\nnull\nnull\n", new String(Files.readAllBytes(Paths.get(output.getAbsolutePath()))));
}

@Test
public void testMainEmpty() throws IOException, GetNothingException {
File input = testFolder.newFile("input.txt");
File output = testFolder.newFile("output.txt");

Main.main(new String[]{input.getAbsolutePath(), output.getAbsolutePath()});

assertEquals("", new String(Files.readAllBytes(Paths.get(output.getAbsolutePath()))));
}

}
Loading