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
38 changes: 37 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,40 @@
/.metadata
/robots/.settings
/robots/bin
eclipse.bat
eclipse.bat
out/
/robots/.classpath
/robots/.project
# Translations
*.mo

# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

# IDEA
.idea/
*.iml

# Maven
target/
103 changes: 103 additions & 0 deletions robots/src/collections/BoundedCircularQueue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package collections;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

public class BoundedCircularQueue<E> extends AbstractQueue<E> {
private final int capacity;
private final Object[] buffer;
private final AtomicInteger head = new AtomicInteger(0);
private final AtomicInteger size = new AtomicInteger(0);

public BoundedCircularQueue(int capacity) {
if (capacity <= 0) throw new IllegalArgumentException("Capacity must be positive");
this.capacity = capacity;
this.buffer = new Object[capacity];
}

@Override
public boolean offer(E e) {
if (e == null) throw new NullPointerException();
while (true) {
int currentSize = size.get();
if (currentSize < capacity) {
if (size.compareAndSet(currentSize, currentSize + 1)) {
int pos = (head.get() + currentSize) % capacity;
buffer[pos] = e;
return true;
}
} else {
int oldHead = head.get();
int newHead = (oldHead + 1) % capacity;
if (head.compareAndSet(oldHead, newHead)) {
buffer[oldHead] = e;
return true;
}
}
}
}

@Override
public E poll() {
while (true) {
int currentSize = size.get();
if (currentSize == 0) return null;
if (size.compareAndSet(currentSize, currentSize - 1)) {
int idx = head.getAndUpdate(h -> (h + 1) % capacity);
@SuppressWarnings("unchecked")
E result = (E) buffer[idx];
buffer[idx] = null;
return result;
}
}
}

@Override
public E peek() {
int currentSize = size.get();
if (currentSize == 0) return null;
@SuppressWarnings("unchecked")
E result = (E) buffer[head.get()];
return result;
}

@Override
public int size() {
return size.get();
}

@Override
public Iterator<E> iterator() {
return snapshot().iterator();
}

public List<E> getAll() {
return snapshot();
}

public List<E> getRange(int start, int end) {
int currentSize = size.get();
if (start < 0 || end > currentSize || start > end)
throw new IndexOutOfBoundsException("Invalid range: " + start + ".." + end);
int currentHead = head.get();
List<E> result = new ArrayList<>(end - start);
for (int i = start; i < end; i++) {
@SuppressWarnings("unchecked")
E element = (E) buffer[(currentHead + i) % capacity];
result.add(element);
}
return Collections.unmodifiableList(result);
}

private List<E> snapshot() {
int currentHead = head.get();
int currentSize = size.get();
List<E> result = new ArrayList<>(currentSize);
for (int i = 0; i < currentSize; i++) {
@SuppressWarnings("unchecked")
E element = (E) buffer[(currentHead + i) % capacity];
result.add(element);
}
return Collections.unmodifiableList(result);
}
}
64 changes: 64 additions & 0 deletions robots/src/collections/BoundedCircularQueueTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package collections;

import org.junit.jupiter.api.Test;
import java.util.*;
import static org.junit.jupiter.api.Assertions.*;

class BoundedCircularQueueTest {

@Test
void testOfferAndPoll() {
BoundedCircularQueue<String> q = new BoundedCircularQueue<>(3);
assertTrue(q.offer("A"));
assertTrue(q.offer("B"));
assertTrue(q.offer("C"));
assertEquals(3, q.size());
assertEquals("A", q.poll());
assertEquals("B", q.peek());
assertEquals(2, q.size());
}

@Test
void testOverflow() {
BoundedCircularQueue<String> q = new BoundedCircularQueue<>(3);
q.offer("A");
q.offer("B");
q.offer("C");
q.offer("D"); // перезаписывает A
assertEquals(3, q.size());
assertEquals("B", q.poll());
assertEquals("C", q.poll());
assertEquals("D", q.poll());
assertNull(q.poll());
}

@Test
void testGetAll() {
BoundedCircularQueue<Integer> q = new BoundedCircularQueue<>(5);
for (int i = 0; i < 5; i++) q.offer(i);
assertEquals(List.of(0, 1, 2, 3, 4), q.getAll());
}

@Test
void testGetRange() {
BoundedCircularQueue<String> q = new BoundedCircularQueue<>(5);
q.offer("a");
q.offer("b");
q.offer("c");
q.offer("d");
assertEquals(List.of("b", "c"), q.getRange(1, 3));
}

@Test
void testIteratorSnapshot() {
BoundedCircularQueue<String> q = new BoundedCircularQueue<>(3);
q.offer("X");
q.offer("Y");
Iterator<String> it = q.iterator();
q.offer("Z"); // добавление не ломает итератор
assertTrue(it.hasNext());
assertEquals("X", it.next());
assertEquals("Y", it.next());
assertFalse(it.hasNext());
}
}
89 changes: 89 additions & 0 deletions robots/src/config/ConfigManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package config;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import log.Logger;

public class ConfigManager {
private static final String CONFIG_FILE = System.getProperty("user.home") + File.separator + ".robot-config.properties";
private final Properties props = new Properties();

public void load() {
File file = new File(CONFIG_FILE);
if (!file.exists()) return;
try (FileInputStream fis = new FileInputStream(file)) {
props.load(fis);
} catch (IOException e) {
Logger.debug("Не удалось загрузить конфигурацию: " + e.getMessage());
}
}

public void save() {
try (FileOutputStream fos = new FileOutputStream(CONFIG_FILE)) {
props.store(fos, "Robot program configuration");
} catch (IOException e) {
Logger.debug("Не удалось сохранить конфигурацию: " + e.getMessage());
}
}

public void setMainWindowBounds(int x, int y, int width, int height, int state) {
setProperty("main.x", x);
setProperty("main.y", y);
setProperty("main.width", width);
setProperty("main.height", height);
setProperty("main.state", state);
}

public int getMainWindowX(int defaultValue) { return getIntProperty("main.x", defaultValue); }
public int getMainWindowY(int defaultValue) { return getIntProperty("main.y", defaultValue); }
public int getMainWindowWidth(int defaultValue) { return getIntProperty("main.width", defaultValue); }
public int getMainWindowHeight(int defaultValue) { return getIntProperty("main.height", defaultValue); }
public int getMainWindowState(int defaultValue) { return getIntProperty("main.state", defaultValue); }

public void setWindowBounds(String windowName, int x, int y, int width, int height, boolean icon, boolean maximized) {
setProperty(key(windowName, "x"), x);
setProperty(key(windowName, "y"), y);
setProperty(key(windowName, "width"), width);
setProperty(key(windowName, "height"), height);
setProperty(key(windowName, "icon"), icon);
setProperty(key(windowName, "maximized"), maximized);
}

public boolean hasWindow(String windowName) {
return props.containsKey(key(windowName, "x"));
}

public int getWindowX(String windowName, int defaultValue) { return getIntProperty(key(windowName, "x"), defaultValue); }
public int getWindowY(String windowName, int defaultValue) { return getIntProperty(key(windowName, "y"), defaultValue); }
public int getWindowWidth(String windowName, int defaultValue) { return getIntProperty(key(windowName, "width"), defaultValue); }
public int getWindowHeight(String windowName, int defaultValue) { return getIntProperty(key(windowName, "height"), defaultValue); }
public boolean getWindowIcon(String windowName, boolean defaultValue) { return getBooleanProperty(key(windowName, "icon"), defaultValue); }
public boolean getWindowMaximized(String windowName, boolean defaultValue) { return getBooleanProperty(key(windowName, "maximized"), defaultValue); }

public void setWindowVisible(String windowName, boolean visible) {
setProperty(key(windowName, "visible"), visible);
}

public boolean getWindowVisible(String windowName, boolean defaultValue) {
return getBooleanProperty(key(windowName, "visible"), defaultValue);
}


private String key(String windowName, String suffix) { return windowName + "." + suffix; }
private void setProperty(String key, int value) { props.setProperty(key, Integer.toString(value)); }
private void setProperty(String key, boolean value) { props.setProperty(key, Boolean.toString(value)); }

private int getIntProperty(String key, int defaultValue) {
String val = props.getProperty(key);
if (val == null) return defaultValue;
try { return Integer.parseInt(val); } catch (NumberFormatException e) { return defaultValue; }
}

private boolean getBooleanProperty(String key, boolean defaultValue) {
String val = props.getProperty(key);
return val == null ? defaultValue : Boolean.parseBoolean(val);
}
}
Loading