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
23 changes: 23 additions & 0 deletions java-data-handling-template.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?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" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter:5.5.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-api:5.5.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.apiguardian:apiguardian-api:1.1.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.opentest4j:opentest4j:1.2.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-commons:1.5.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-params:5.5.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-engine:5.5.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-engine:1.5.2" level="project" />
</component>
</module>
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
package com.epam.izh.rd.online.repository;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

public class SimpleFileRepository implements FileRepository {

/**
Expand All @@ -10,7 +20,26 @@ public class SimpleFileRepository implements FileRepository {
*/
@Override
public long countFilesInDirectory(String path) {
return 0;
File file = new File(path);
if (!(file.isDirectory() || file.isFile()))
{
URL url = getClass().getResource("/" + path);
file = new File(url.getPath());
}
File[] directory = file.listFiles();

long count = 0;

if (directory != null) {
for (File item : directory) {
if (!item.isFile()) {
count += countFilesInDirectory(item.getPath());
} else {
count++;
}
}
}
return count;
}

/**
Expand All @@ -21,7 +50,28 @@ public long countFilesInDirectory(String path) {
*/
@Override
public long countDirsInDirectory(String path) {
return 0;
long count = 0;

File file = new File(path);
if (!(file.isDirectory() || file.isFile()))
{
URL url = getClass().getResource("/" + path);
file = new File(url.getPath());
count++;
}

File[] directory = file.listFiles();


if (directory != null) {
for (File item : directory) {
if (item.isDirectory()) {
count++;
count += countDirsInDirectory(item.getPath());
}
}
}
return count;
}

/**
Expand All @@ -32,9 +82,17 @@ public long countDirsInDirectory(String path) {
*/
@Override
public void copyTXTFiles(String from, String to) {
return;
File file = new File(to);
createFile(file.getParent(), file.getName());

try {
Files.copy(Paths.get(from), Paths.get(to), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException ioEx) {
ioEx.printStackTrace();
}
}


/**
* Метод создает файл на диске с расширением txt
*
Expand All @@ -44,6 +102,20 @@ public void copyTXTFiles(String from, String to) {
*/
@Override
public boolean createFile(String path, String name) {
URL url = getClass().getResource("/");
File folder = new File(url.getPath() + "/" + path);

File file = new File(folder.getPath() + "/" + name);
boolean isDirectoryCreated = !new File(path).isDirectory();

try {
if (isDirectoryCreated) {
Files.createDirectory(Paths.get(path));
}
return file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}

Expand All @@ -55,6 +127,15 @@ public boolean createFile(String path, String name) {
*/
@Override
public String readFileFromResources(String fileName) {
return null;
StringBuilder stringBuilder = new StringBuilder();
try (BufferedReader is = new BufferedReader(new FileReader("src/main/resources/" + fileName))) {
String line;
while ((line = is.readLine()) != null) {
stringBuilder.append(line);
}
} catch (IOException ioex) {
ioex.printStackTrace();
}
return stringBuilder.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.epam.izh.rd.online.service;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

class SieveEratosthenes {

private static class PrimePair {
BigInteger prime;
BigInteger lastCrossed;

PrimePair(BigInteger prime, BigInteger lastCrossed) {
this.prime = prime;
this.lastCrossed = lastCrossed;
}
}

private List<PrimePair> primes;

SieveEratosthenes() {
primes = new ArrayList<>();
primes.add(new PrimePair(new BigInteger("2"), new BigInteger("2")));
primes.add(new PrimePair(new BigInteger("3"), new BigInteger("3")));
}

BigInteger fillNPrimes(int n) {
if (n >= 0 && n <= 1) {
return primes.get(n).prime;
}
while (primes.size() <= n) {
addNextPrime();
}
return primes.get(primes.size() - 1).prime;
}

private void addNextPrime() {
BigInteger candidate = primes.get(primes.size() - 1).prime.add(new BigInteger("2"));
for (int i = 1; i < primes.size(); i++) {
PrimePair p = primes.get(i);
while (p.lastCrossed.compareTo(candidate) < 0) {
p.lastCrossed = p.lastCrossed.add(p.prime);
}
if (p.lastCrossed.compareTo(candidate) == 0) {
candidate = candidate.add(new BigInteger("2"));
i = -1;
}
}
primes.add(new PrimePair(candidate, candidate));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;

public class SimpleBigNumbersService implements BigNumbersService {

/**
* Метод делит первое число на второе с заданной точностью
* Например 1/3 с точностью 2 = 0.33
*
* @param range точность
* @return результат
*/
@Override
public BigDecimal getPrecisionNumber(int a, int b, int range) {
return null;
BigDecimal[] bigDecimals = {new BigDecimal(a), new BigDecimal(b)};
return bigDecimals[0].divide(bigDecimals[1], range, RoundingMode.HALF_UP);
}

/**
Expand All @@ -24,6 +27,8 @@ public BigDecimal getPrecisionNumber(int a, int b, int range) {
*/
@Override
public BigInteger getPrimaryNumber(int range) {
return null;
SieveEratosthenes sieveEratosthenes = new SieveEratosthenes();
return sieveEratosthenes.fillNPrimes(range);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.GregorianCalendar;

public class SimpleDateService implements DateService {

Expand All @@ -14,7 +15,8 @@ public class SimpleDateService implements DateService {
*/
@Override
public String parseDate(LocalDate localDate) {
return null;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
return localDate.format(formatter);
}

/**
Expand All @@ -25,7 +27,8 @@ public String parseDate(LocalDate localDate) {
*/
@Override
public LocalDateTime parseString(String string) {
return null;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
return LocalDateTime.parse(string, formatter);
}

/**
Expand All @@ -37,7 +40,8 @@ public LocalDateTime parseString(String string) {
*/
@Override
public String convertToCustomFormat(LocalDate localDate, DateTimeFormatter formatter) {
return null;

return localDate.format(formatter);
}

/**
Expand All @@ -47,7 +51,14 @@ public String convertToCustomFormat(LocalDate localDate, DateTimeFormatter forma
*/
@Override
public long getNextLeapYear() {
return 0;
GregorianCalendar calendar = (GregorianCalendar) GregorianCalendar.getInstance();
int leapYear = calendar.get(GregorianCalendar.YEAR);
for (; ; ) {
leapYear++;
if (calendar.isLeapYear(leapYear)) {
return leapYear;
}
}
}

/**
Expand All @@ -57,7 +68,12 @@ public long getNextLeapYear() {
*/
@Override
public long getSecondsInYear(int year) {
return 0;
GregorianCalendar calendar = (GregorianCalendar) GregorianCalendar.getInstance();
if (calendar.isLeapYear(year)) {
return 366 * 24 * 60 * 60;
} else {
return 365 * 24 * 60 * 60;
}
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.epam.izh.rd.online.service;

import com.epam.izh.rd.online.repository.SimpleFileRepository;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SimpleRegExpService implements RegExpService {

/**
Expand All @@ -11,7 +15,16 @@ public class SimpleRegExpService implements RegExpService {
*/
@Override
public String maskSensitiveData() {
return null;
StringBuilder stringBuilder = new StringBuilder(
new SimpleFileRepository().readFileFromResources("sensitive_data.txt")
);

Pattern pattern = Pattern.compile("((\\d){4}\\s?){4}");
Matcher matcher = pattern.matcher(stringBuilder.toString());
while (matcher.find()) {
stringBuilder.replace(matcher.start() + 4, matcher.end() - 5, " **** **** ");
}
return stringBuilder.toString();
}

/**
Expand All @@ -22,6 +35,23 @@ public String maskSensitiveData() {
*/
@Override
public String replacePlaceholders(double paymentAmount, double balance) {
return null;
StringBuilder stringBuilder = new StringBuilder(
new SimpleFileRepository().readFileFromResources("sensitive_data.txt")
);

Pattern pattern = Pattern.compile("[$][{]{1}\\w+[}]{1}");
Matcher matcher = pattern.matcher(stringBuilder.toString());

while (matcher.find()) {
if (stringBuilder.substring(matcher.start() + 2, matcher.end() - 1).equals("payment_amount")) {
stringBuilder.replace(matcher.start(), matcher.end(), String.format("%.0f", paymentAmount));
matcher = pattern.matcher(stringBuilder.toString());
} else if (stringBuilder.substring(matcher.start() + 2, matcher.end() - 1).equals("balance")) {
stringBuilder.replace(matcher.start(), matcher.end(), String.format("%.0f", balance));
matcher = pattern.matcher(stringBuilder.toString());
}
}
return stringBuilder.toString();
}

}
Loading