Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
7e0afc6
Create .gitkeep
Shopin-Igor Feb 13, 2025
8d49ecf
Create .gitkeep
Shopin-Igor Feb 13, 2025
964043f
Add files via upload
Shopin-Igor Feb 13, 2025
42ae381
Create .gitkeep
Shopin-Igor Feb 13, 2025
4ad64c4
Add files via upload
Shopin-Igor Feb 13, 2025
6445bbb
Create .gitkeep
Shopin-Igor Feb 13, 2025
c532d82
Add files via upload
Shopin-Igor Feb 13, 2025
40754c0
Create .gitkeep
Shopin-Igor Feb 13, 2025
88a6c23
Add files via upload
Shopin-Igor Feb 13, 2025
f951e1b
Add files via upload
Shopin-Igor Feb 27, 2025
54119b2
Merge pull request #3 from Shopin-Igor/main
Shopin-Igor Feb 27, 2025
6d3af45
Add files via upload
Shopin-Igor Mar 20, 2025
210bd79
Add files via upload
Shopin-Igor Apr 18, 2025
361415d
Add files via upload
Shopin-Igor Apr 18, 2025
8c564bc
Add files via upload
Shopin-Igor Apr 18, 2025
2a4b05e
Add files via upload
Shopin-Igor Apr 18, 2025
b058f42
Merge pull request #9 from Shopin-Igor/Homework5
Shopin-Igor Apr 18, 2025
2e66098
Merge pull request #8 from Shopin-Igor/Homework5
Shopin-Igor Apr 18, 2025
aedf34d
hw5
Shopin-Igor Apr 18, 2025
4419183
Homework 6
Shopin-Igor May 2, 2025
277e402
Create test_github
Shopin-Igor May 31, 2025
0826ebd
Update test_github
Shopin-Igor May 31, 2025
7e12bc9
Project
Shopin-Igor Jun 8, 2025
52b75dd
Add files via upload
Shopin-Igor Jun 8, 2025
b4e65eb
Merge pull request #12 from Shopin-Igor/Project
Shopin-Igor Dec 14, 2025
e72dd2d
Merge pull request #4 from Shopin-Igor/nomework1
Shopin-Igor Dec 14, 2025
fc87f81
Merge pull request #5 from Shopin-Igor/Shopin-Igor-HW4
Shopin-Igor Dec 14, 2025
da50455
Merge pull request #10 from Shopin-Igor/hw5
Shopin-Igor Dec 14, 2025
7677afd
Merge pull request #11 from Shopin-Igor/homework-6
Shopin-Igor Dec 14, 2025
4777b02
Merge pull request #13 from Shopin-Igor/Homwork-7
Shopin-Igor Dec 14, 2025
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
Binary file added Homework7/cache_fib#5.ser
Binary file not shown.
Binary file added Homework7/cache_fib#6.ser
Binary file not shown.
17 changes: 17 additions & 0 deletions Homework7/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?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>org.example</groupId>
<artifactId>Homework7</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

</project>
39 changes: 39 additions & 0 deletions Homework7/src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import generator.RandomObjectGenerator;
import model.MyClass;
import model.MyRecord;
import proxy.CacheProxy;
import annotations.Cache;
import proxy.FibCalculator;

public class Main {
public static void main(String[] args) {
// генератор объектов
RandomObjectGenerator rog = new RandomObjectGenerator();

var myClass = rog.nextObject(MyClass.class, "create"); //через фабрику
System.out.println("MyClass (factory): " + myClass);


var myRecord = rog.nextObject(MyRecord.class); //через конструктор
System.out.println("MyRecord (random): " + myRecord);

// обычная реализация
FibCalculator original = new FibCalculator() {
@Override
@Cache(persist = true)
public long fib(int number) {
if (number <= 1) return number;
return fib(number - 1) + fib(number - 2);
}
};

// кэш-прокси
FibCalculator proxy = CacheProxy.create(original, FibCalculator.class);

// вызовы с кэшированием
System.out.println("fib(5): " + proxy.fib(5));
System.out.println("fib(6): " + proxy.fib(6));
System.out.println("fib(5) again (from cache): " + proxy.fib(5));
}

}
9 changes: 9 additions & 0 deletions Homework7/src/main/java/annotations/Cache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package annotations;

import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Cache {
boolean persist() default false;
}
9 changes: 9 additions & 0 deletions Homework7/src/main/java/annotations/Max.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package annotations;

import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Max {
long value();
}
9 changes: 9 additions & 0 deletions Homework7/src/main/java/annotations/Min.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package annotations;

import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Min {
long value();
}
7 changes: 7 additions & 0 deletions Homework7/src/main/java/annotations/NotNull.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package annotations;

import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface NotNull {}
55 changes: 55 additions & 0 deletions Homework7/src/main/java/generator/RandomObjectGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package generator;

import annotations.Max;
import annotations.Min;

import java.lang.reflect.*;
import java.util.Random;

public class RandomObjectGenerator {
private Random random = new Random();

public <T> T nextObject(Class<T> clazz) {
return nextObject(clazz, null);
}

public <T> T nextObject(Class<T> clazz, String factoryMethodName) {
try {
if (factoryMethodName != null) {
Method method = clazz.getMethod(factoryMethodName);
return clazz.cast(method.invoke(null));
}

Constructor<?> constructor = clazz.getDeclaredConstructors()[0];
constructor.setAccessible(true);

Parameter[] params = constructor.getParameters();
Object[] args = new Object[params.length];

for (int i = 0; i < params.length; i++) {
args[i] = generateValue(params[i]);
}

return clazz.cast(constructor.newInstance(args));
} catch (Exception e) {
throw new RuntimeException(e);
}
}

private Object generateValue(Parameter parameter) {
Class<?> type = parameter.getType();
AnnotatedElement element = parameter;

if (type == int.class || type == Integer.class) {
long min = 0, max = 100;
if (element.isAnnotationPresent(Min.class))
min = element.getAnnotation(Min.class).value();
if (element.isAnnotationPresent(Max.class))
max = element.getAnnotation(Max.class).value();
return (int)(min + random.nextInt((int)(max - min + 1)));
} else if (type == String.class) {
return "str_" + random.nextInt(1000);
}
return null;
}
}
29 changes: 29 additions & 0 deletions Homework7/src/main/java/model/MyClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package model;

import annotations.Min;
import annotations.Max;
import annotations.NotNull;

public class MyClass {
@NotNull
@Min(10)
@Max(99)
private Integer age;

@NotNull
private String name;

public MyClass(Integer age, String name) {
this.age = age;
this.name = name;
}

public static MyClass create() {
return new MyClass(42, "FactoryCreated");
}

@Override
public String toString() {
return "MyClass{age=" + age + ", name='" + name + "'}";
}
}
13 changes: 13 additions & 0 deletions Homework7/src/main/java/model/MyRecord.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package model;

import annotations.*;

public record MyRecord(
@NotNull
@Min(10)
@Max(50)
Integer count,

@NotNull
String description
) {}
61 changes: 61 additions & 0 deletions Homework7/src/main/java/proxy/CacheProxy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package proxy;

import annotations.Cache;

import java.io.*;
import java.lang.reflect.*;
import java.util.HashMap;
import java.util.Map;

public class CacheProxy {
@SuppressWarnings("unchecked")
public static <T> T create(T target, Class<?> interf) {
return (T) Proxy.newProxyInstance(
interf.getClassLoader(),
new Class<?>[]{interf},
new CacheHandler(target)
);
}

private static class CacheHandler implements InvocationHandler {
private final Object target;
private final Map<String, Object> cache = new HashMap<>();

public CacheHandler(Object target) {
this.target = target;
}

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (!method.isAnnotationPresent(Cache.class))
return method.invoke(target, args);

String key = method.getName() + "#" + (args != null ? args[0] : "null");
Cache cacheAnn = method.getAnnotation(Cache.class);

if (cache.containsKey(key)) {
return cache.get(key);
}

File file = new File("cache_" + key + ".ser");
if (cacheAnn.persist() && file.exists()) {
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {
Object result = ois.readObject();
cache.put(key, result);
return result;
}
}

Object result = method.invoke(target, args);
cache.put(key, result);

if (cacheAnn.persist()) {
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file))) {
oos.writeObject(result);
}
}

return result;
}
}
}
8 changes: 8 additions & 0 deletions Homework7/src/main/java/proxy/FibCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package proxy;

import annotations.Cache;

public interface FibCalculator {
@Cache(persist = true)
long fib(int number);
}
Binary file added Homework7/target/classes/Main$1.class
Binary file not shown.
Binary file added Homework7/target/classes/Main.class
Binary file not shown.
Binary file added Homework7/target/classes/annotations/Cache.class
Binary file not shown.
Binary file added Homework7/target/classes/annotations/Max.class
Binary file not shown.
Binary file added Homework7/target/classes/annotations/Min.class
Binary file not shown.
Binary file added Homework7/target/classes/annotations/NotNull.class
Binary file not shown.
Binary file not shown.
Binary file added Homework7/target/classes/model/MyClass.class
Binary file not shown.
Binary file added Homework7/target/classes/model/MyRecord.class
Binary file not shown.
Binary file not shown.
Binary file added Homework7/target/classes/proxy/CacheProxy.class
Binary file not shown.
Binary file added Homework7/target/classes/proxy/FibCalculator.class
Binary file not shown.
14 changes: 14 additions & 0 deletions Task1/Atomic_Counter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package Task1;
import java.util.concurrent.atomic.AtomicInteger;

public class Atomic_Counter {
private final AtomicInteger count = new AtomicInteger(0);
public void increment() {
count.getAndIncrement();
}


public int getCount() {
return count.get();
}
}
34 changes: 34 additions & 0 deletions Task1/Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package Task1;
import java.io.*;
import java.net.ConnectException;
import java.net.Socket;
import java.util.Scanner;


public class Client {
private static final String HOST = "localhost";
private static final int PORT = 55535;

public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
while (true) {
System.out.print("Ваня: ");
String input = scanner.nextLine().trim();
if (input.equalsIgnoreCase("exit")) break;

try (Socket socket = new Socket(HOST, PORT);
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream in = new ObjectInputStream(socket.getInputStream())) {
out.writeObject(input);
out.flush();
String response = (String) in.readObject();
System.out.println("Сервер: " + response);
} catch (ConnectException e) {
System.out.println("Сервер перегружен. Ожидайте...");
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
}
}
30 changes: 30 additions & 0 deletions Task1/ComputerClubAnalytics.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package Task1;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

public class ComputerClubAnalytics {
public static String calculateAverageSessionTime(List<String> sessions) {
if (sessions == null || sessions.isEmpty()) {
return "0ч 0м";
}

Duration totalDuration = Duration.ZERO;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd, HH:mm");

for (String session : sessions) {
String[] parts = session.split(" - ");
if (parts.length != 2) continue;

LocalDateTime start = LocalDateTime.parse(parts[0], formatter);
LocalDateTime end = LocalDateTime.parse(parts[1], formatter);
totalDuration = totalDuration.plus(Duration.between(start, end));
}

Duration averageDuration = totalDuration.dividedBy(sessions.size());
long hours = averageDuration.toHours();
long minutes = averageDuration.toMinutes() % 60;
return hours + "ч " + minutes + "м";
}
}
19 changes: 19 additions & 0 deletions Task1/ReetnrantLock_Counter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package Task1;
import java.util.concurrent.locks.ReentrantLock;

public class ReetnrantLock_Counter {
private int count = 0;
private final ReentrantLock lock = new ReentrantLock();
public void increment() {
lock.lock();
try {
count++;
} finally {
lock.unlock();
}
}

public int getCount() {
return count;
}
}
21 changes: 21 additions & 0 deletions Task1/Semaphore_Counter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package Task1;
import java.util.concurrent.Semaphore;

public class Semaphore_Counter {
private int count = 0;
private final Semaphore semaphore = new Semaphore(1); // Используем стандартный Semaphore

public void increment() {
try {
semaphore.acquire(); // Метод acquire() из java.util.concurrent.Semaphore
count++;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
semaphore.release(); // Метод release() из java.util.concurrent.Semaphore
}
}

public int getCount() {
return count;
}
Loading