Started working on power-framework 2.0.0
This commit is contained in:
parent
273e1a8647
commit
a3717d963a
4
CHANGELOG.md
Normal file
4
CHANGELOG.md
Normal file
@ -0,0 +1,4 @@
|
||||
# Changelog
|
||||
# 0.1.0
|
||||
* ?
|
||||
|
37
README.md
37
README.md
@ -1,7 +1,34 @@
|
||||
# power-framework
|
||||
|
||||
## Requirements
|
||||
* Maven
|
||||
* Java 8 (for example: Amazon Corretto 8 - https://docs.aws.amazon.com/corretto/latest/corretto-8-ug/downloads-list.html)
|
||||
|
||||
|
||||
## Important notes to read first:
|
||||
|
||||
1. This is a hobbyist project.
|
||||
2. This project is licenced as LGPL 2.1, which also means it is distributed without any warranty.
|
||||
3. In case, you find a bug or propose an improvement:
|
||||
* Raise an issue or participate a new discussion.
|
||||
|
||||
## About
|
||||
|
||||
Power Framework is a Java library.
|
||||
|
||||
It contains several modules: view, collections, json, time, db, persistence, random, reflection, sql, xml and web and others.
|
||||
|
||||
View is an abstraction of JavaFX. Window class may be the most used class of this module and is used as a place for everything user sees. Window class uses custom decorations, system decorations are not used.
|
||||
|
||||
Collections contains full implementation of some collection type like linked list, queue, stack, list, map, set, properties, tree and others.
|
||||
|
||||
Json is used to represent json object and json array as a Java Object. Json files can be parsed to JsonObject instance. Every instance of JsonObject or JsonArray can be printed to minimal or pretty String.
|
||||
|
||||
DB is used to give abstraction for jdbc and SQLite, hides implementation. Database package can be used to store permanent data.
|
||||
|
||||
Persistence is used to dynamically manage SQLite database, create table ddl, insert, update and delete statements are automatically generated from Java classes using reflection. Persistence behaves maybe like the Hibernate library.
|
||||
|
||||
Random is used to generate pseudorandom numbers and is based on linear congruent function and using seeds.
|
||||
|
||||
Time contains some classes used to represent date and time without time zone information, universal date and time, date and time with time zone information. There are also some classes representing durations and period. Time module is partially an abstraction on Java SE time classes, but the interface to use it is a little different.
|
||||
|
||||
Reflection is a abstraction of Java SE reflection classes.
|
||||
|
||||
SQL generates sql Statements.
|
||||
|
||||
Xml is used to generate xml output by Java classes. Web uses xml and creates html pages.
|
||||
|
154
pom.xml
154
pom.xml
@ -5,7 +5,7 @@
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation;
|
||||
License as published by the Free Software Foundation;
|
||||
version 2.1 of the License only.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
@ -22,52 +22,148 @@
|
||||
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>
|
||||
|
||||
<parent>
|
||||
<groupId>org.nanoboot.essential</groupId>
|
||||
<artifactId>nanoboot-parent</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<groupId>org.nanoboot.powerframework</groupId>
|
||||
<artifactId>power-framework</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<packaging>jar</packaging>
|
||||
<version>2.0.0-SNAPSHOT</version>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<name>Power Framework</name>
|
||||
<description>A library with many possibilities</description>
|
||||
<description>
|
||||
Power Framework is a bundle of Java many purposes libraries
|
||||
like working with texts, randomness, collections, json format,
|
||||
time, database, files, desktop graphics and some others.
|
||||
</description>
|
||||
|
||||
<url>http://robertvokac/products/power-framework</url>
|
||||
<inceptionYear>2016</inceptionYear>
|
||||
<modules>
|
||||
<module>power-core</module>
|
||||
<module>power-text</module>
|
||||
<module>power-random</module>
|
||||
<module>power-utils</module>
|
||||
<module>power-collections</module>
|
||||
|
||||
<module>power-json</module>
|
||||
<module>power-time</module>
|
||||
<module>power-log</module>
|
||||
<module>power-io</module>
|
||||
<module>power-web</module>
|
||||
|
||||
<module>power-wiki</module>
|
||||
<module>power-bean</module>
|
||||
<module>power-redmond</module>
|
||||
<module>power-shark</module>
|
||||
<module>power-sound</module>
|
||||
|
||||
<module>power-stat</module>
|
||||
<module>power-sql</module>
|
||||
<module>power-db</module>
|
||||
<module>power-persistence</module>
|
||||
<module>power-svg</module>
|
||||
|
||||
<module>power-view</module>
|
||||
<module>power-xml</module>
|
||||
<module>power-reflection</module>
|
||||
<module>power-mail</module>
|
||||
<module>power-blockchain</module>
|
||||
|
||||
<module>power-security</module>
|
||||
<!-- TODO:
|
||||
power-concurrency
|
||||
power-net
|
||||
-->
|
||||
</modules>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<power.version>2.0.0-SNAPSHOT</power.version>
|
||||
<checkstyle.skip>true</checkstyle.skip><!-- TODO: make false-->
|
||||
<javase.version>8</javase.version>
|
||||
</properties>
|
||||
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- Other dependencies -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.xerial</groupId>
|
||||
<artifactId>sqlite-jdbc</artifactId>
|
||||
<version>3.39.3.0</version>
|
||||
</dependency>
|
||||
<!-- <dependency>
|
||||
<groupId>org.openjfx</groupId>
|
||||
<artifactId>javafx-controls</artifactId>
|
||||
<version>19</version>
|
||||
</dependency>-->
|
||||
</dependencies>
|
||||
<contributors>
|
||||
<contributor>
|
||||
<name>Robert Vokac</name>
|
||||
<email>robertvokac@nanoboot.org</email>
|
||||
<roles>
|
||||
<role>developer</role>
|
||||
<role>project manager</role>
|
||||
</roles>
|
||||
</contributor>
|
||||
</contributors>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.jacoco</groupId>
|
||||
<artifactId>jacoco-maven-plugin</artifactId>
|
||||
<version>${jacoco-maven-plugin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>prepare-agent</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
<!-- attached to Maven test phase -->
|
||||
<execution>
|
||||
<id>report</id>
|
||||
<phase>test</phase>
|
||||
<goals>
|
||||
<goal>report</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-checkstyle-plugin</artifactId>
|
||||
<configuration>
|
||||
<skip>${checkstyle.skip}</skip>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>${javase.version}</source>
|
||||
<target>${javase.version}</target>
|
||||
<annotationProcessorPaths>
|
||||
<path>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>${lombok.version}</version>
|
||||
</path>
|
||||
</annotationProcessorPaths>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<!-- Other dependencies -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit4.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>${lombok.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
</project>
|
||||
|
54
power-ai/pom.xml
Normal file
54
power-ai/pom.xml
Normal file
@ -0,0 +1,54 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
power-framework: Java library with many purposes of usage.
|
||||
Copyright (C) 2016-2022 the original author or authors.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation;
|
||||
version 2.1 of the License only.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
-->
|
||||
<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>
|
||||
|
||||
<parent>
|
||||
<groupId>org.nanoboot.powerframework</groupId>
|
||||
<artifactId>power-framework</artifactId>
|
||||
<version>2.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>power-ai</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>Power AI</name>
|
||||
<description>Artificial intelligence tools for the Power library</description>
|
||||
|
||||
<dependencies>
|
||||
<!-- Power dependencies -->
|
||||
<dependency>
|
||||
<groupId>org.nanoboot.powerframework</groupId>
|
||||
<artifactId>power-core</artifactId>
|
||||
<version>${power.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Other dependencies -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
29
power-ai/src/main/java/module-info.java
Normal file
29
power-ai/src/main/java/module-info.java
Normal file
@ -0,0 +1,29 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
module powerframework.ai {
|
||||
|
||||
}
|
54
power-bean/pom.xml
Normal file
54
power-bean/pom.xml
Normal file
@ -0,0 +1,54 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
power-framework: Java library with many purposes of usage.
|
||||
Copyright (C) 2016-2022 the original author or authors.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation;
|
||||
version 2.1 of the License only.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
-->
|
||||
<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>
|
||||
|
||||
<parent>
|
||||
<groupId>org.nanoboot.powerframework</groupId>
|
||||
<artifactId>power-framework</artifactId>
|
||||
<version>2.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>power-bean</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>Power Bean</name>
|
||||
<description>Dependency functionality for the Power library</description>
|
||||
|
||||
<dependencies>
|
||||
<!-- Power dependencies -->
|
||||
<dependency>
|
||||
<groupId>org.nanoboot.powerframework</groupId>
|
||||
<artifactId>power-core</artifactId>
|
||||
<version>${power.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Other dependencies -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
29
power-bean/src/main/java/module-info.java
Normal file
29
power-bean/src/main/java/module-info.java
Normal file
@ -0,0 +1,29 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
module powerframework.bean {
|
||||
|
||||
}
|
0
power-bean/src/main/resources/.gitkeep
Normal file
0
power-bean/src/main/resources/.gitkeep
Normal file
122
power-blockchain/pom.xml
Normal file
122
power-blockchain/pom.xml
Normal file
@ -0,0 +1,122 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
power-framework: Java library with many purposes of usage.
|
||||
Copyright (C) 2016-2022 the original author or authors.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation;
|
||||
version 2.1 of the License only.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
-->
|
||||
<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>
|
||||
|
||||
<parent>
|
||||
<groupId>org.nanoboot.powerframework</groupId>
|
||||
<artifactId>power-framework</artifactId>
|
||||
<version>2.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<groupId>org.nanoboot.powerframework</groupId>
|
||||
<artifactId>power-blockchain</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>Power Blockchain</name>
|
||||
<description>Blockchain library</description>
|
||||
|
||||
<build>
|
||||
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<version>3.3.0</version>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>org.nanoboot.powerframework.blockchain.orig.core.Main</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
<descriptorRefs>
|
||||
<descriptorRef>jar-with-dependencies</descriptorRef>
|
||||
</descriptorRefs>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>make-assembly</id> <!-- this is used for inheritance merges -->
|
||||
<phase>package</phase> <!-- bind to the packaging phase -->
|
||||
<goals>
|
||||
<goal>single</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>org.nanoboot.powerframework.blockchain.orig.core.Main</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<!-- Power dependencies -->
|
||||
<dependency>
|
||||
<groupId>org.nanoboot.powerframework</groupId>
|
||||
<artifactId>power-core</artifactId>
|
||||
<version>${power.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.nanoboot.powerframework</groupId>
|
||||
<artifactId>power-time</artifactId>
|
||||
<version>${power.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.nanoboot.powerframework</groupId>
|
||||
<artifactId>power-json</artifactId>
|
||||
<version>${power.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.nanoboot.powerframework</groupId>
|
||||
<artifactId>power-random</artifactId>
|
||||
<version>${power.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.nanoboot.powerframework</groupId>
|
||||
<artifactId>power-security</artifactId>
|
||||
<version>${power.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Other dependencies -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.10</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
34
power-blockchain/src/main/java/module-info.java
Normal file
34
power-blockchain/src/main/java/module-info.java
Normal file
@ -0,0 +1,34 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
module powerframework.blockchain {
|
||||
requires lombok;
|
||||
requires powerframework.core;
|
||||
requires powerframework.time;
|
||||
requires powerframework.json;
|
||||
requires powerframework.random;
|
||||
requires powerframework.security;
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.api;
|
||||
|
||||
import org.nanoboot.powerframework.blockchain.core.Block;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
public interface BlockDeserializer {
|
||||
Block deserialize(String serializedBlock);
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.api;
|
||||
|
||||
import org.nanoboot.powerframework.blockchain.core.Block;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
public interface BlockSerializer {
|
||||
String serialize(Block block);
|
||||
}
|
@ -0,0 +1,132 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.core;
|
||||
|
||||
import org.nanoboot.powerframework.blockchain.api.BlockSerializer;
|
||||
import org.nanoboot.powerframework.json.JsonObject;
|
||||
import org.nanoboot.powerframework.json.JsonObjectSerializable;
|
||||
import org.nanoboot.powerframework.security.hash.api.HashCalculator;
|
||||
import org.nanoboot.powerframework.time.moment.UniversalDateTime;
|
||||
import lombok.Getter;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
public class Block implements JsonObjectSerializable {
|
||||
private static final String BLOCKCHAIN_PROTOCOL = "blockchainProtocol";
|
||||
|
||||
private static final String HEIGHT = "height";
|
||||
|
||||
private static final String PREVIOUS_HASH = "previousHash";
|
||||
private static final String TIMESTAMP = "timestamp";
|
||||
private static final String DIFFICULTY = "difficulty";
|
||||
private static final String DATA = "data";
|
||||
private static final String NONCE = "nonce";
|
||||
|
||||
@Getter
|
||||
private final BlockchainProtocol blockchainProtocol;
|
||||
|
||||
@Getter
|
||||
private final long height;
|
||||
@Getter
|
||||
private final String previousHash;
|
||||
@Getter
|
||||
private final String timeStamp;
|
||||
@Getter
|
||||
private final String difficultyTarget;
|
||||
@Getter
|
||||
private final byte[] data;
|
||||
|
||||
@Getter
|
||||
private long nonce;
|
||||
|
||||
//
|
||||
@Getter
|
||||
private String hash = null;
|
||||
|
||||
//Block Constructor.
|
||||
public Block(BlockchainProtocol blockchainProtocol,
|
||||
long height,
|
||||
String previousHash,
|
||||
String difficultyTarget,
|
||||
byte[] data) {
|
||||
this.blockchainProtocol=blockchainProtocol;
|
||||
this.height = height;
|
||||
this.previousHash = previousHash;
|
||||
this.timeStamp = UniversalDateTime.now().toString();
|
||||
this.difficultyTarget = difficultyTarget;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
//Calculate new hash based on blocks contents
|
||||
public String calculateHash(HashCalculator hashCalculator, BlockSerializer blockSerializer) {
|
||||
String serializedBlock = blockSerializer.serialize(this);
|
||||
String calculatedhash = hashCalculator.hash(serializedBlock);
|
||||
return calculatedhash;
|
||||
}
|
||||
|
||||
//Increases nonce value until hash target is reached.
|
||||
public void mineBlock(HashCalculator hashCalculator, BlockSerializer blockSerializer) {
|
||||
String target = this.difficultyTarget;
|
||||
|
||||
for (long i = 0; i <= Long.MAX_VALUE; i++) {
|
||||
this.nonce = i;
|
||||
if (nonce == Integer.MAX_VALUE) {
|
||||
throw new BlockChainException("Nonce was not found.");
|
||||
}
|
||||
if (nonce % 1000000 == 0) {
|
||||
System.out.println("Nonce # " + nonce);
|
||||
}
|
||||
|
||||
|
||||
|
||||
String hash = calculateHash(hashCalculator, blockSerializer);
|
||||
|
||||
if (hashCalculator.compareHexNumbers(hash, this.getDifficultyTarget()) <= 0) {
|
||||
System.out.println("Found nonce: " + nonce + " A new block was just mined: " + getHash() + "\n" /*+ block.toJsonObject().toPrettyString()*/);
|
||||
System.out.println("Block Mined!!! : " + hash);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonObject toJsonObject() {
|
||||
|
||||
JsonObject jo = new JsonObject();
|
||||
|
||||
jo.add(BLOCKCHAIN_PROTOCOL, blockchainProtocol.toJsonObject());
|
||||
|
||||
jo.add(HEIGHT, height);
|
||||
|
||||
jo.add(PREVIOUS_HASH, previousHash);
|
||||
jo.add(TIMESTAMP, timeStamp);
|
||||
jo.add(DIFFICULTY, difficultyTarget);
|
||||
jo.add(DATA, new String(data));
|
||||
jo.add(NONCE, nonce);
|
||||
|
||||
return jo;
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.core;
|
||||
|
||||
import org.nanoboot.powerframework.core.PowerException;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
public class BlockChainException extends PowerException {
|
||||
public BlockChainException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.core;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
@AllArgsConstructor
|
||||
public class BlockchainEngine {
|
||||
@Getter
|
||||
private final String protocolName;
|
||||
private List<BlockchainEngineVersion> blockchainEngineVersionList = new ArrayList<>();
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.core;
|
||||
|
||||
import org.nanoboot.powerframework.blockchain.api.BlockSerializer;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
@AllArgsConstructor
|
||||
public class BlockchainEngineVersion {
|
||||
@Getter
|
||||
private final String version;
|
||||
@Getter
|
||||
private final String hashCalculatorName;
|
||||
@Getter
|
||||
private final String targetDifficulty;
|
||||
@Getter
|
||||
private final BlockSerializer blockSerializer;
|
||||
@Getter
|
||||
private final long validFromBlock;
|
||||
@Getter
|
||||
private final long validUntilBlock;
|
||||
|
||||
public String getMayorVersion() {
|
||||
return version.split(("\\."))[0];
|
||||
}
|
||||
public String getMinorVersion() {
|
||||
return version.split(("\\."))[1];
|
||||
}
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.core;
|
||||
|
||||
import org.nanoboot.powerframework.json.JsonObject;
|
||||
import org.nanoboot.powerframework.json.JsonObjectSerializable;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
@AllArgsConstructor
|
||||
public class BlockchainProtocol implements JsonObjectSerializable {
|
||||
private static final String PROTOCOL_NAME = "protocolName";
|
||||
private static final String PROTOCOL_MAYOR_VERSION = "protocolMayorVersion";
|
||||
private static final String PROTOCOL_MINOR_VERSION = "protocolMinorVersion";
|
||||
@Getter
|
||||
private final String protocolName;
|
||||
@Getter
|
||||
private final int protocolMayorVersion;
|
||||
@Getter
|
||||
private final int protocolMinorVersion;
|
||||
@Override
|
||||
public JsonObject toJsonObject() {
|
||||
|
||||
JsonObject jo = new JsonObject();
|
||||
|
||||
jo.add(PROTOCOL_NAME, protocolName);
|
||||
jo.add(PROTOCOL_MAYOR_VERSION, protocolMayorVersion);
|
||||
jo.add(PROTOCOL_MINOR_VERSION, protocolMinorVersion);
|
||||
|
||||
return jo;
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.impl;
|
||||
|
||||
import org.nanoboot.powerframework.blockchain.api.BlockSerializer;
|
||||
import org.nanoboot.powerframework.blockchain.core.Block;
|
||||
import org.nanoboot.powerframework.json.JsonObject;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
public class JsonBlockSerializer implements BlockSerializer {
|
||||
|
||||
@Override
|
||||
public String serialize(Block block) {
|
||||
JsonObject jsonObject = block.toJsonObject();
|
||||
return jsonObject.toMinimalString();
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.impl;
|
||||
|
||||
import org.nanoboot.powerframework.blockchain.api.BlockSerializer;
|
||||
import org.nanoboot.powerframework.blockchain.core.Block;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
public class StringSerializer implements BlockSerializer {
|
||||
|
||||
public static final String SEPARATOR = "::";
|
||||
|
||||
@Override
|
||||
public String serialize(Block block) {
|
||||
return block.getBlockchainProtocol().toString() + SEPARATOR +
|
||||
block.getHeight() + SEPARATOR +
|
||||
block.getPreviousHash() + SEPARATOR +
|
||||
block.getTimeStamp() + SEPARATOR +
|
||||
block.getDifficultyTarget() + SEPARATOR +
|
||||
new String(block.getData()) + SEPARATOR +
|
||||
block.getNonce();
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.orig.api;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
public interface BlockData {
|
||||
String convertToString();
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.orig.api;
|
||||
|
||||
import org.nanoboot.powerframework.blockchain.orig.base.Block;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
public interface BlockDeserializer {
|
||||
Block deserialize(String serializedBlock);
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.orig.api;
|
||||
|
||||
import org.nanoboot.powerframework.blockchain.orig.base.Block;
|
||||
import org.nanoboot.powerframework.blockchain.orig.base.BlockFragment;
|
||||
import org.nanoboot.powerframework.blockchain.orig.base.BlockType;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
public interface BlockMiner {
|
||||
Block mine(BlockFragment blockFragment, BlockType blockType);
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.orig.api;
|
||||
|
||||
import org.nanoboot.powerframework.blockchain.orig.base.Block;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
public interface BlockSerializer {
|
||||
String serialize(Block block);
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.orig.api;
|
||||
|
||||
import org.nanoboot.powerframework.blockchain.orig.base.Block;
|
||||
import org.nanoboot.powerframework.blockchain.orig.base.BlockType;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
public interface BlockValidator {
|
||||
BlockType getBlockType();
|
||||
void validate(Block block);
|
||||
}
|
@ -0,0 +1,117 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.orig.base;
|
||||
|
||||
import org.nanoboot.powerframework.blockchain.orig.api.BlockData;
|
||||
import org.nanoboot.powerframework.blockchain.core.BlockChainException;
|
||||
import org.nanoboot.powerframework.json.JsonObject;
|
||||
import org.nanoboot.powerframework.json.JsonObjectSerializable;
|
||||
import lombok.Getter;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
public class Block implements JsonObjectSerializable {
|
||||
public static final String BLOCK_TYPE_ID = "blockTypeId";
|
||||
public static final String BLOCK_HEADER = "blockHeader";
|
||||
public static final String BLOCK_DATA = "blockData";
|
||||
public static final String NONCE = "nonce";
|
||||
|
||||
@Getter
|
||||
private BlockHeader blockHeader;
|
||||
|
||||
@Getter
|
||||
private BlockData blockData;
|
||||
|
||||
private long nonce;
|
||||
|
||||
@Getter
|
||||
private String hash;
|
||||
@Getter
|
||||
private boolean locked = false;
|
||||
|
||||
@Getter
|
||||
private BlockType internalBlockType;
|
||||
|
||||
public Block(BlockHeader blockHeader, JsonObject additionBlockHeader, BlockData blockData) {
|
||||
this.blockHeader = blockHeader;
|
||||
|
||||
this.blockData = blockData;
|
||||
}
|
||||
|
||||
public void injectBlockType(BlockType blockType) {
|
||||
this.internalBlockType = blockType;
|
||||
}
|
||||
|
||||
public String calculateHashTest(long nonce) {
|
||||
this.nonce = nonce;
|
||||
String hashedSerializedBlock = internalCalculateHash();
|
||||
this.nonce = 0;//todo
|
||||
return hashedSerializedBlock;
|
||||
}
|
||||
|
||||
private String internalCalculateHash() {
|
||||
if (nonce == 0) {
|
||||
throw new BlockChainException("Nonce cannot be zero.");
|
||||
}
|
||||
if (locked) {
|
||||
throw new BlockChainException("This block is already locked. Cannot set nonce and calculate nonce.");
|
||||
}
|
||||
|
||||
if (internalBlockType == null) {
|
||||
throw new BlockChainException("internalBlockType was not injected.");
|
||||
}
|
||||
String serializedBlock = this.internalBlockType.getBlockSerializer().serialize(this);
|
||||
|
||||
String hashedSerializedBlock = this.internalBlockType.getHashCalculator().hash(serializedBlock);
|
||||
|
||||
//System.out.println("internalCalculateHash - hash for nonce " + nonce + " = " + hashedSerializedBlock);
|
||||
return hashedSerializedBlock;
|
||||
}
|
||||
|
||||
public void lock(long nonce) {
|
||||
this.nonce = nonce;
|
||||
String hashedSerializedBlock = internalCalculateHash();
|
||||
this.hash = hashedSerializedBlock;
|
||||
|
||||
this.locked = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonObject toJsonObject() {
|
||||
if (nonce == 0) {
|
||||
throw new BlockChainException("Nonce cannot be zero, if json object is created from this block instance.");
|
||||
}
|
||||
if (internalBlockType == null) {
|
||||
throw new BlockChainException("internalBlockType was not injected.");
|
||||
}
|
||||
JsonObject jo = new JsonObject();
|
||||
jo.add(BLOCK_TYPE_ID, internalBlockType.getBlockTypeId());
|
||||
jo.add(BLOCK_HEADER, blockHeader.toJsonObject());
|
||||
|
||||
jo.add(BLOCK_DATA, blockData.convertToString());
|
||||
jo.add(NONCE, nonce);
|
||||
return jo;
|
||||
}
|
||||
}
|
@ -0,0 +1,100 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.orig.base;
|
||||
|
||||
import org.nanoboot.powerframework.blockchain.core.BlockChainException;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
public class BlockChain {
|
||||
private String blockChainEngineName;
|
||||
private List<Block> blocks = new ArrayList<>();
|
||||
|
||||
public int getTotalHeight() {
|
||||
return blocks.size() - 1;
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return blocks.isEmpty();
|
||||
}
|
||||
|
||||
public Block getGenesisBlock() {
|
||||
if (isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return getBlock(0);
|
||||
}
|
||||
|
||||
public Block getLastBlock() {
|
||||
if (isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return getBlock(blocks.size() - 1);
|
||||
}
|
||||
|
||||
public List<Block> getLastXBlocks(int xCount) {
|
||||
List<Block> result = new ArrayList<>();
|
||||
|
||||
if (isEmpty()) {
|
||||
return result;
|
||||
}
|
||||
if (xCount <= blocks.size()) {
|
||||
for (Block b : blocks) {
|
||||
result.add(b);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
for (int i = blocks.size() - xCount; i <= (blocks.size() - 1); i++) {
|
||||
result.add(blocks.get(i));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public void addBlock(Block block) {
|
||||
if (!block.isLocked()) {
|
||||
throw new BlockChainException("Block is not locked. It cannot be added");
|
||||
}
|
||||
if (blocks.isEmpty() && block.getBlockHeader().getHeight() != 0) {
|
||||
throw new BlockChainException("New block is a genesis block, but its height is not zero.");
|
||||
}
|
||||
Block lastBlock = getLastBlock();
|
||||
String previousBlockHash = lastBlock == null ? "0" : lastBlock.getHash();
|
||||
|
||||
if (!block.getBlockHeader().getPreviousHash().equals(previousBlockHash)) {
|
||||
throw new BlockChainException("Block previous hash is not equal to the hash of the previous block. It cannot be added to the blockchain.");
|
||||
}
|
||||
blocks.add(block);
|
||||
}
|
||||
|
||||
public Block getBlock(int blockHeight) {
|
||||
if (blockHeight >= blocks.size()) {
|
||||
throw new BlockChainException("There is no block with height " + blockHeight);
|
||||
}
|
||||
return blocks.get(blockHeight);
|
||||
}
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.orig.base;
|
||||
|
||||
import org.nanoboot.powerframework.blockchain.core.BlockChainException;
|
||||
import lombok.AllArgsConstructor;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
@AllArgsConstructor
|
||||
public abstract class BlockChainEngine {
|
||||
private String blockChainEngineName;
|
||||
protected BlockChain blockChain;
|
||||
protected BlockTypeVersionList blockTypeVersionList;
|
||||
|
||||
public void addData(String data) {
|
||||
BlockFragment blockFragment = createNewBlockFragment(data);
|
||||
BlockType blockType = blockTypeVersionList.findBlockType(blockFragment.getBlockHeader().getBlockTypeId());
|
||||
if (blockType == null) {
|
||||
throw new BlockChainException("blockType " + blockFragment.getBlockHeader().getBlockTypeId() + " was not found.");
|
||||
}
|
||||
|
||||
Block block = blockType.getBlockMiner().mine(blockFragment, blockType);
|
||||
addBlock(block);
|
||||
}
|
||||
|
||||
public abstract BlockFragment createNewBlockFragment(String data);
|
||||
|
||||
public void addBlock(Block newBlock) {
|
||||
blockChain.addBlock(newBlock);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.orig.base;
|
||||
|
||||
import org.nanoboot.powerframework.blockchain.orig.api.BlockData;
|
||||
import org.nanoboot.powerframework.json.JsonObject;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
@AllArgsConstructor
|
||||
public class BlockFragment {
|
||||
@Getter
|
||||
private BlockHeader blockHeader;
|
||||
|
||||
@Getter
|
||||
private BlockData blockData;
|
||||
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.orig.base;
|
||||
|
||||
import org.nanoboot.powerframework.json.JsonObject;
|
||||
import org.nanoboot.powerframework.json.JsonObjectSerializable;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
@AllArgsConstructor
|
||||
public class BlockHeader implements JsonObjectSerializable {
|
||||
public static final String BLOCK_TYPE_ID = "blockTypeId";
|
||||
public static final String HEIGHT = "height";
|
||||
public static final String TIME_STAMP = "timeStamp";
|
||||
public static final String PREVIOUS_HASH = "previousHash";
|
||||
@Getter
|
||||
private String blockTypeId;
|
||||
@Getter
|
||||
private int height;
|
||||
@Getter
|
||||
private long timeStamp;
|
||||
@Getter
|
||||
private String previousHash;
|
||||
@Getter
|
||||
private String difficulty;
|
||||
|
||||
public JsonObject toJsonObject() {
|
||||
JsonObject jo = new JsonObject();
|
||||
jo.add(BLOCK_TYPE_ID, blockTypeId);
|
||||
jo.add(HEIGHT, height);
|
||||
jo.add(TIME_STAMP, timeStamp);
|
||||
jo.add(PREVIOUS_HASH, previousHash);
|
||||
return jo;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.orig.base;
|
||||
|
||||
import org.nanoboot.powerframework.blockchain.orig.api.BlockDeserializer;
|
||||
import org.nanoboot.powerframework.blockchain.orig.api.BlockMiner;
|
||||
import org.nanoboot.powerframework.blockchain.orig.api.BlockSerializer;
|
||||
import org.nanoboot.powerframework.security.hash.api.HashCalculator;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
@AllArgsConstructor
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
public class BlockType {
|
||||
@Getter
|
||||
private String type;
|
||||
@Getter
|
||||
private int mayorVersion;
|
||||
@Getter
|
||||
private int minorVersion;
|
||||
@Getter
|
||||
private HashCalculator hashCalculator;
|
||||
@Getter
|
||||
private BlockSerializer blockSerializer;
|
||||
@Getter
|
||||
private BlockDeserializer blockDeserializer;
|
||||
@Getter
|
||||
BlockMiner blockMiner;
|
||||
public String getBlockTypeId() {
|
||||
return type + ":" + mayorVersion + ":" + minorVersion;
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.orig.base;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
public class BlockTypeVersionList {
|
||||
private Map<String, BlockType> blockTypes;
|
||||
|
||||
public BlockTypeVersionList(List<BlockType> blockTypesList) {
|
||||
blockTypes = new HashMap<>();
|
||||
for (BlockType bt : blockTypesList) {
|
||||
blockTypes.put(bt.getBlockTypeId(), bt);
|
||||
}
|
||||
}
|
||||
|
||||
public BlockType findBlockType(String blockTypeId) {
|
||||
return blockTypes.get(blockTypeId);
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.orig.cash;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
public class Transaction {
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.orig.cash;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
public class TransactionValidator {
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.orig.cash;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
public class Wallet {
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.orig.core;
|
||||
|
||||
import org.nanoboot.powerframework.blockchain.orig.base.BlockChain;
|
||||
import org.nanoboot.powerframework.blockchain.orig.impl.TestCash_BlockChainEngine;
|
||||
import org.nanoboot.powerframework.random.generators.linearcongruent.combined.w5.W5RandomGenerator;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
// HashCalculator.main(null);
|
||||
//
|
||||
// if(true) return;
|
||||
BlockChain blockChain = new BlockChain();
|
||||
TestCash_BlockChainEngine bce = new TestCash_BlockChainEngine(blockChain);
|
||||
bce.addData("ahoj");
|
||||
bce.addData("pavle");
|
||||
bce.addData("leo");
|
||||
for(int i =0;i<100000000;i++){
|
||||
bce.addData(W5RandomGenerator.getStaticInstance().nextText(8));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.orig.impl;
|
||||
|
||||
import org.nanoboot.powerframework.blockchain.orig.api.BlockSerializer;
|
||||
import org.nanoboot.powerframework.blockchain.orig.base.Block;
|
||||
import org.nanoboot.powerframework.json.JsonObject;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
public class JsonBlockSerializer implements BlockSerializer {
|
||||
|
||||
@Override
|
||||
public String serialize(Block block) {
|
||||
JsonObject jsonObject = block.toJsonObject();
|
||||
//System.out.println(jsonObject.toMinimalString());
|
||||
return jsonObject.toMinimalString();
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.orig.impl;
|
||||
|
||||
import org.nanoboot.powerframework.blockchain.orig.api.BlockData;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
@AllArgsConstructor
|
||||
public class SimpleBlockData implements BlockData {
|
||||
@Getter
|
||||
private final String string;
|
||||
@Override
|
||||
public String convertToString() {
|
||||
return string;
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.orig.impl;
|
||||
|
||||
import org.nanoboot.powerframework.blockchain.orig.api.BlockData;
|
||||
import org.nanoboot.powerframework.blockchain.orig.base.BlockHeader;
|
||||
import org.nanoboot.powerframework.blockchain.orig.base.Block;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
public class TestCash_1_0_Block extends Block {
|
||||
public TestCash_1_0_Block(BlockHeader blockHeader, BlockData blockData) {
|
||||
super(blockHeader, null, blockData);
|
||||
}
|
||||
}
|
@ -0,0 +1,95 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.orig.impl;
|
||||
|
||||
import org.nanoboot.powerframework.blockchain.orig.api.BlockMiner;
|
||||
import org.nanoboot.powerframework.blockchain.orig.base.Block;
|
||||
import org.nanoboot.powerframework.blockchain.orig.base.BlockFragment;
|
||||
import org.nanoboot.powerframework.blockchain.orig.base.BlockType;
|
||||
import org.nanoboot.powerframework.blockchain.core.BlockChainException;
|
||||
import org.nanoboot.powerframework.security.hash.api.HashCalculator;
|
||||
import org.nanoboot.powerframework.time.duration.Duration;
|
||||
import org.nanoboot.powerframework.time.duration.StopWatch;
|
||||
import org.nanoboot.powerframework.time.utils.TimeUnit;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
public class TestCash_1_0_BlockMiner implements BlockMiner {
|
||||
public static List<Integer> lastFiveBlockMiningTimesInMilliseconds = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public Block mine(BlockFragment blockFragment, BlockType blockType) {
|
||||
|
||||
TestCash_1_0_Block block = new TestCash_1_0_Block(blockFragment.getBlockHeader(), blockFragment.getBlockData());
|
||||
block.injectBlockType(blockType);
|
||||
|
||||
////System.out.println("Starting mining block # " + block.getBlockHeader().getHeight());
|
||||
//System.out.println("Note: One block should be mined in 1 minute, but the last time was " + countOfSecondsToMineLastBlock + " second(s).");
|
||||
//System.out.println("Starting mining block # " + block.getBlockHeader().getHeight());
|
||||
StopWatch sw = new StopWatch();
|
||||
|
||||
//System.out.println("start: " + UniversalDateTime.now().toString());
|
||||
sw.start();
|
||||
|
||||
////System.out.println("Searching hash equal or less than " + block.getBlockHeader().getDifficulty());
|
||||
for (long nonce = 1; nonce <= Long.MAX_VALUE; nonce++) {
|
||||
if (nonce == Integer.MAX_VALUE) {
|
||||
throw new BlockChainException("Nonce was not found.");
|
||||
}
|
||||
if (nonce % 1000000 == 0) {
|
||||
System.out.println("Nonce # " + nonce);
|
||||
}
|
||||
|
||||
|
||||
HashCalculator hashCalculator = block.getInternalBlockType().getHashCalculator();
|
||||
String hash = block.calculateHashTest(nonce);
|
||||
//System.err.println("hashCalculator.compareHexNumbers("+hash+"+, block.getBlockHeader().getDifficulty()="+block.getBlockHeader().getDifficulty()+")="+hashCalculator.compareHexNumbers(hash, block.getBlockHeader().getDifficulty()));
|
||||
if (hashCalculator.compareHexNumbers(hash, block.getBlockHeader().getDifficulty()) <= 0) {
|
||||
|
||||
//System.err.println("hashCalculator.compareHexNumbers("+hash+"+, block.getBlockHeader().getDifficulty()="+block.getBlockHeader().getDifficulty()+")="+hashCalculator.compareHexNumbers(hash, block.getBlockHeader().getDifficulty()));
|
||||
block.lock(nonce);
|
||||
////System.out.println("Found nonce: " + nonce + " A new block was just mined: " + block.getHash() + "\n" /*+ block.toJsonObject().toPrettyString()*/);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//System.out.println("end: " + UniversalDateTime.now().toString());
|
||||
sw.stop();
|
||||
Duration lastDuration = sw.getCurrentDuration();
|
||||
if (lastFiveBlockMiningTimesInMilliseconds.size() == 1000) {
|
||||
lastFiveBlockMiningTimesInMilliseconds.remove(0);
|
||||
}
|
||||
lastFiveBlockMiningTimesInMilliseconds.add((int) lastDuration.toTotal(TimeUnit.MILLISECOND));
|
||||
//countOfSecondsToMineLastBlock = (int) lastDuration.toTotal(TimeUnit.SECOND);
|
||||
if (block.getBlockHeader().getHeight() % 100 == 0)
|
||||
System.out.println("Mining block " + block.getBlockHeader().getHeight() + " + took " + lastDuration.toString() + " .");
|
||||
return block;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.orig.impl;
|
||||
|
||||
import org.nanoboot.powerframework.blockchain.orig.base.BlockType;
|
||||
import org.nanoboot.powerframework.security.hash.locator.HashCalculatorLocator;
|
||||
import org.nanoboot.powerframework.security.hash.locator.HashImpl;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
public class TestCash_1_0_BlockType extends BlockType {
|
||||
private static final String TYPE = "test_cash";
|
||||
private static final int MAYOR_VERSION = 1;
|
||||
private static final int MINOR_VERSION = 0;
|
||||
public TestCash_1_0_BlockType() {
|
||||
super(TYPE, MAYOR_VERSION, MINOR_VERSION, HashCalculatorLocator.locate(HashImpl.SHA_256), new JsonBlockSerializer(), null, new TestCash_1_0_BlockMiner());
|
||||
}
|
||||
}
|
@ -0,0 +1,136 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.blockchain.orig.impl;
|
||||
|
||||
import org.nanoboot.powerframework.blockchain.orig.base.*;
|
||||
import org.nanoboot.powerframework.security.hash.api.HashCalculator;
|
||||
import org.nanoboot.powerframework.time.duration.Duration;
|
||||
import org.nanoboot.powerframework.time.moment.UniversalDateTime;
|
||||
import org.nanoboot.powerframework.time.utils.TimeUnit;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.util.List;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
public class TestCash_BlockChainEngine extends BlockChainEngine {
|
||||
|
||||
public static final String TEST_CASH = "TestCash";
|
||||
public static final int COUNT_OF_MILLISECONDS_TO_MINE_A_BLOCK = 10;
|
||||
//public static final String DEFAULT_DIFFICULTY = "0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
|
||||
public static final String DEFAULT_DIFFICULTY = "00184f329993e8d38cd0a31969b54367b4fc5af6ed86d827c1343dbf8b66a4a5";
|
||||
|
||||
public static float DIFFICULTY_PERCENT_CHANGE = (float) 1;
|
||||
|
||||
public TestCash_BlockChainEngine(BlockChain blockChain) {
|
||||
super(TEST_CASH, blockChain, new BlockTypeVersionList(List.of(new TestCash_1_0_BlockType())));
|
||||
}
|
||||
|
||||
@Override
|
||||
public BlockFragment createNewBlockFragment(String data) {
|
||||
Block lastBlock = blockChain.getLastBlock();
|
||||
int newBlockHeight = lastBlock == null ? 0 : lastBlock.getBlockHeader().getHeight() + 1;
|
||||
long newTimeStamp = UniversalDateTime.now().toLong();
|
||||
String previousBlockHash = lastBlock == null ? "0" : lastBlock.getHash();
|
||||
String oldDifficulty = lastBlock == null ? DEFAULT_DIFFICULTY : lastBlock.getBlockHeader().getDifficulty();
|
||||
String newDifficulty = oldDifficulty;
|
||||
|
||||
int newDifficultyPeriod = 1000;
|
||||
if (lastBlock != null && newBlockHeight % newDifficultyPeriod == 0) {
|
||||
|
||||
List<Block> lastBlocks = blockChain.getLastXBlocks(newDifficultyPeriod);
|
||||
int lastBlocksCount = lastBlocks.size();
|
||||
Block blockStart = lastBlocks.get(0);
|
||||
Block blockEnd = lastBlocks.get(lastBlocks.size() - 1);
|
||||
long moment1 = blockStart.getBlockHeader().getTimeStamp();
|
||||
long moment2 = blockEnd.getBlockHeader().getTimeStamp();
|
||||
UniversalDateTime udt1 = new UniversalDateTime(moment1);
|
||||
UniversalDateTime udt2 = new UniversalDateTime(moment2);
|
||||
Duration duration = new org.nanoboot.powerframework.time.duration.Period(udt1, udt2).getDuration();
|
||||
duration.toTotal(TimeUnit.SECOND);
|
||||
double averageCountOfMillisecondsToMineABlockForTenLastBlocks = (duration.toTotal(TimeUnit.SECOND) / (lastBlocksCount - 1));
|
||||
|
||||
int countX = 0;
|
||||
int sum = 0;
|
||||
for (Integer i : TestCash_1_0_BlockMiner.lastFiveBlockMiningTimesInMilliseconds) {
|
||||
sum = sum + i;
|
||||
countX = countX + 1;
|
||||
}
|
||||
System.out.println("sumInSeconds="+((double)sum));
|
||||
|
||||
averageCountOfMillisecondsToMineABlockForTenLastBlocks = ((((double) sum) / ((double) countX)) /*/ 1*/);
|
||||
System.out.println("averageCountOfMillisecondsToMineABlockForTenLastBlocks=" + averageCountOfMillisecondsToMineABlockForTenLastBlocks);
|
||||
boolean plus = false;
|
||||
if(((int)Math.round(averageCountOfMillisecondsToMineABlockForTenLastBlocks))==COUNT_OF_MILLISECONDS_TO_MINE_A_BLOCK) {
|
||||
DIFFICULTY_PERCENT_CHANGE=1f/16;
|
||||
} else {
|
||||
if(DIFFICULTY_PERCENT_CHANGE != (float) 1f/1f) {
|
||||
DIFFICULTY_PERCENT_CHANGE = (float) 1f/1f;
|
||||
}
|
||||
}
|
||||
if (averageCountOfMillisecondsToMineABlockForTenLastBlocks < COUNT_OF_MILLISECONDS_TO_MINE_A_BLOCK) {
|
||||
plus = false;
|
||||
System.out.println("Difficulty +++ increasing");
|
||||
|
||||
}
|
||||
if (averageCountOfMillisecondsToMineABlockForTenLastBlocks > COUNT_OF_MILLISECONDS_TO_MINE_A_BLOCK) {
|
||||
plus = true;
|
||||
System.out.println("Difficulty --- decreasing");
|
||||
}
|
||||
// if (DIFFICULTY_PERCENT_CHANGE >= 16) {
|
||||
// DIFFICULTY_PERCENT_CHANGE = 4;
|
||||
// }
|
||||
|
||||
System.out.println("averageCountOfSecondsToMineABlockForTenLastBlocks=" + averageCountOfMillisecondsToMineABlockForTenLastBlocks);
|
||||
if (averageCountOfMillisecondsToMineABlockForTenLastBlocks != COUNT_OF_MILLISECONDS_TO_MINE_A_BLOCK) {
|
||||
//DIFFICULTY_PERCENT_CHANGE = DIFFICULTY_PERCENT_CHANGE * 2;
|
||||
double realDifficultyPercentChange = DIFFICULTY_PERCENT_CHANGE;
|
||||
double more = averageCountOfMillisecondsToMineABlockForTenLastBlocks > COUNT_OF_MILLISECONDS_TO_MINE_A_BLOCK ? averageCountOfMillisecondsToMineABlockForTenLastBlocks : COUNT_OF_MILLISECONDS_TO_MINE_A_BLOCK;
|
||||
double less = averageCountOfMillisecondsToMineABlockForTenLastBlocks < COUNT_OF_MILLISECONDS_TO_MINE_A_BLOCK ? averageCountOfMillisecondsToMineABlockForTenLastBlocks : COUNT_OF_MILLISECONDS_TO_MINE_A_BLOCK;
|
||||
|
||||
if ((less / more) < 0.5) {
|
||||
realDifficultyPercentChange = 50;
|
||||
}
|
||||
double change = 1 + ((plus ? 1 : (-1)) * (realDifficultyPercentChange / 100));
|
||||
// if((double)(averageCountOfSecondsToMineABlockForTenLastBlocks) / (double)(COUNT_OF_SECONDS_TO_MINE_A_BLOCK)<0.9 || (double)(averageCountOfSecondsToMineABlockForTenLastBlocks) / (double)(COUNT_OF_SECONDS_TO_MINE_A_BLOCK)> 1.1){
|
||||
// change = 1 + ((plus ? 1 : (-1)) * (25f / 100));
|
||||
// }
|
||||
System.out.println(" Changing to " + change);
|
||||
BlockType blockType = blockTypeVersionList.findBlockType("test_cash:1:0");
|
||||
HashCalculator hashCalculator = blockType.getHashCalculator();
|
||||
String oldDifficultyAsDec = hashCalculator.convertHexStringToDecString(oldDifficulty);
|
||||
System.out.println("oldDifficultyAsDec=" + oldDifficultyAsDec);
|
||||
BigInteger bi = new BigDecimal(oldDifficultyAsDec).multiply(new BigDecimal(change)).toBigInteger();
|
||||
System.out.println("newDifficultyAsDec=" + bi.toString());
|
||||
newDifficulty = hashCalculator.convertDecStringToHexString(bi.toString());
|
||||
System.err.println("NEW_DIFFICULTY = " + newDifficulty);
|
||||
} else {
|
||||
//DIFFICULTY_PERCENT_CHANGE = DIFFICULTY_PERCENT_CHANGE / 2;
|
||||
}
|
||||
}
|
||||
BlockHeader blockHeader = new BlockHeader("test_cash:1:0", newBlockHeight, newTimeStamp, previousBlockHash, newDifficulty);
|
||||
return new BlockFragment(blockHeader, new SimpleBlockData(data));
|
||||
}
|
||||
}
|
0
power-blockchain/src/main/resources/.gitkeep
Normal file
0
power-blockchain/src/main/resources/.gitkeep
Normal file
73
power-collections/pom.xml
Normal file
73
power-collections/pom.xml
Normal file
@ -0,0 +1,73 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
power-framework: Java library with many purposes of usage.
|
||||
Copyright (C) 2016-2022 the original author or authors.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation;
|
||||
version 2.1 of the License only.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
-->
|
||||
<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>
|
||||
|
||||
<parent>
|
||||
<groupId>org.nanoboot.powerframework</groupId>
|
||||
<artifactId>power-framework</artifactId>
|
||||
<version>2.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>power-collections</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>Power Collections</name>
|
||||
<description>Collections functionality for the Power library</description>
|
||||
|
||||
<properties>
|
||||
<checkstyle.skip>true</checkstyle.skip>
|
||||
<!-- todo true is only temporary to be able to do the snapshot build, but the styles and formatting must be resolved in future. -->
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- Power dependencies -->
|
||||
<dependency>
|
||||
<groupId>org.nanoboot.powerframework</groupId>
|
||||
<artifactId>power-core</artifactId>
|
||||
<version>${power.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.nanoboot.powerframework</groupId>
|
||||
<artifactId>power-random</artifactId>
|
||||
<version>${power.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.nanoboot.powerframework</groupId>
|
||||
<artifactId>power-utils</artifactId>
|
||||
<version>${power.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Other dependencies -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
37
power-collections/src/main/java/module-info.java
Normal file
37
power-collections/src/main/java/module-info.java
Normal file
@ -0,0 +1,37 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
//TODO: collections should have methods returning this
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
module powerframework.collections {
|
||||
exports org.nanoboot.powerframework.collections;
|
||||
exports org.nanoboot.powerframework.collections.arrays;
|
||||
requires powerframework.core;
|
||||
requires powerframework.random;
|
||||
requires powerframework.utils;
|
||||
requires lombok;
|
||||
}
|
@ -0,0 +1,176 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.collections;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.nanoboot.powerframework.collections.internal.AbstractTreeNode;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param <T>
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
public final class BinaryTreeNode<T> extends AbstractTreeNode<T> {
|
||||
/**
|
||||
* Left node.
|
||||
*/
|
||||
@Getter
|
||||
private BinaryTreeNode<T> left = null;
|
||||
/**
|
||||
* Right node.
|
||||
*/
|
||||
@Getter
|
||||
private BinaryTreeNode<T> right = null;
|
||||
/**
|
||||
* Type of the node.
|
||||
*
|
||||
*/
|
||||
@Getter
|
||||
@Setter(AccessLevel.PACKAGE)
|
||||
private BinaryTreeNodeType type = BinaryTreeNodeType.UNDEFINED;
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public BinaryTreeNode() {
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param valueIn the value
|
||||
*/
|
||||
public BinaryTreeNode(final T valueIn) {
|
||||
this.setValue(valueIn);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param valueIn value
|
||||
* @param leftIn left child
|
||||
* @param rightIn right child
|
||||
*/
|
||||
public BinaryTreeNode(
|
||||
final T valueIn,
|
||||
final BinaryTreeNode<T> leftIn,
|
||||
final BinaryTreeNode<T> rightIn) {
|
||||
this(valueIn);
|
||||
this.setLeft(leftIn);
|
||||
this.setRight(rightIn);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param valueIn value
|
||||
* @param typeIn type of node
|
||||
* @param nodeIn node to add
|
||||
*/
|
||||
public BinaryTreeNode(
|
||||
final T valueIn,
|
||||
final BinaryTreeNodeType typeIn,
|
||||
final BinaryTreeNode<T> nodeIn) {
|
||||
this(valueIn);
|
||||
this.setNode(typeIn, nodeIn);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param typeIn node type
|
||||
* @return
|
||||
*/
|
||||
public BinaryTreeNode<T> getNode(
|
||||
final BinaryTreeNodeType typeIn) {
|
||||
switch (typeIn) {
|
||||
case LEFT: return getLeft();
|
||||
case RIGHT: return getRight();
|
||||
default:
|
||||
String msg = "Unsupported "
|
||||
+ BinaryTreeNode.class.getName() + type;
|
||||
throw new CollectionException(msg);
|
||||
}
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param node left node to add
|
||||
*/
|
||||
public void setLeft(final BinaryTreeNode<T> node) {
|
||||
setNode(BinaryTreeNodeType.LEFT, node);
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param node left node to add
|
||||
*/
|
||||
public void setRight(final BinaryTreeNode<T> node) {
|
||||
setNode(BinaryTreeNodeType.RIGHT, node);
|
||||
}
|
||||
/**
|
||||
* @param typeIn node type
|
||||
* @param nodeIn node to set
|
||||
* @return
|
||||
*/
|
||||
public void setNode(
|
||||
final BinaryTreeNodeType typeIn,
|
||||
final BinaryTreeNode<T> nodeIn) {
|
||||
switch (typeIn) {
|
||||
case LEFT: this.left = nodeIn; break;
|
||||
case RIGHT: this.right = nodeIn; break;
|
||||
default:
|
||||
String msg = "Unsupported "
|
||||
+ BinaryTreeNode.class.getName() + type;
|
||||
throw new CollectionException(msg);
|
||||
}
|
||||
nodeIn.setParent(this);
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @return true, if this node has left node, otherwise false
|
||||
*/
|
||||
public boolean hasLeft() {
|
||||
return hasNode(BinaryTreeNodeType.LEFT);
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @return true, if this node has right node, otherwise false
|
||||
*/
|
||||
public boolean hasRight() {
|
||||
return hasNode(BinaryTreeNodeType.RIGHT);
|
||||
}
|
||||
/**
|
||||
* @param typeIn node type
|
||||
* @return
|
||||
*/
|
||||
public boolean hasNode(
|
||||
final BinaryTreeNodeType typeIn) {
|
||||
BinaryTreeNode<T> node = getNode(typeIn);
|
||||
return node != null;
|
||||
}
|
||||
/**
|
||||
* @param type node type
|
||||
* @return true, if this node has right node, otherwise false
|
||||
*/
|
||||
public boolean has(final BinaryTreeNodeType type) {
|
||||
return getRight() != null;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.collections;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
public enum BinaryTreeNodeType {
|
||||
/**
|
||||
* Undefined node.
|
||||
*/
|
||||
UNDEFINED,
|
||||
/**
|
||||
* Root node.
|
||||
*/
|
||||
ROOT,
|
||||
/**
|
||||
* Left node.
|
||||
*/
|
||||
LEFT,
|
||||
/**
|
||||
* Right node.
|
||||
*/
|
||||
RIGHT;
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.collections;
|
||||
|
||||
import org.nanoboot.powerframework.core.PowerException;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
public class CollectionException extends PowerException {
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param messageIn message describing this exception
|
||||
*/
|
||||
public CollectionException(final String messageIn) {
|
||||
super(messageIn);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,319 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.collections;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.nanoboot.powerframework.collections.internal.AbstractLinkedList;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
/**
|
||||
* Represents double linked list data structure
|
||||
* TODO !!!
|
||||
*
|
||||
* @param <E> The type of the values stored in this list.
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
public class DoubleLinkedList<E> extends AbstractLinkedList<E>
|
||||
implements Iterable<E> {
|
||||
//https://www.javatpoint.com/doubly-linked-list
|
||||
//https://www.studytonight.com/data-structures/doubly-linked-list
|
||||
//https://www.geeksforgeeks.org/doubly-linked-list/
|
||||
//https://www.tutorialspoint.com/data_structures_algorithms/doubly_linked_list_algorithm.htm
|
||||
|
||||
/**
|
||||
* The reference to the first node of this list.
|
||||
*/
|
||||
@Getter(AccessLevel.PRIVATE)
|
||||
@Setter(AccessLevel.PRIVATE)
|
||||
private DoubleLinkedListNode<E> firstNode = null;
|
||||
/**
|
||||
* The reference to the last node of this list.
|
||||
*/
|
||||
@Getter(AccessLevel.PRIVATE)
|
||||
@Setter(AccessLevel.PRIVATE)
|
||||
private DoubleLinkedListNode<E> lastNode = null;
|
||||
|
||||
/**
|
||||
* Iterator.
|
||||
*
|
||||
* @param <E> Node type
|
||||
*/
|
||||
private class DoubleLinkedListIterator<E>
|
||||
implements Iterator<E> {
|
||||
/**
|
||||
* Current node.
|
||||
*/
|
||||
@Getter(AccessLevel.PRIVATE)
|
||||
private DoubleLinkedListNode<E> currentNode;
|
||||
|
||||
/**
|
||||
* @param node start node
|
||||
*/
|
||||
DoubleLinkedListIterator(final DoubleLinkedListNode<E> node) {
|
||||
this.currentNode = new DoubleLinkedListNode<>(null);
|
||||
this.currentNode.setNext(node);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return currentNode.hasNext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() {
|
||||
if (!hasNext()) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
this.currentNode = currentNode.getNext();
|
||||
return this.getCurrentNode().getValue();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterator. Iterates from end to start.
|
||||
*
|
||||
* @param <E> Node type
|
||||
*/
|
||||
private class ReversDoubleLinkedListIterator<E>
|
||||
implements Iterator<E> {
|
||||
/**
|
||||
* Current node.
|
||||
*/
|
||||
@Getter(AccessLevel.PRIVATE)
|
||||
private DoubleLinkedListNode<E> currentNode;
|
||||
|
||||
/**
|
||||
* @param lastNodeIn end node
|
||||
*/
|
||||
ReversDoubleLinkedListIterator(
|
||||
final DoubleLinkedListNode<E> lastNodeIn) {
|
||||
this.currentNode = new DoubleLinkedListNode<>(null);
|
||||
this.currentNode.setPrevious(lastNodeIn);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return currentNode.hasPrevious();
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() {
|
||||
if (!hasNext()) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
this.currentNode = currentNode.getPrevious();
|
||||
return this.getCurrentNode().getValue();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return iterator for this list
|
||||
*/
|
||||
@Override
|
||||
public Iterator<E> iterator() {
|
||||
return new DoubleLinkedListIterator(this.firstNode);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return reverse iterator for this list
|
||||
*/
|
||||
public Iterator<E> reverseIterator() {
|
||||
return new ReversDoubleLinkedListIterator(this.firstNode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes all elements of this linked list.
|
||||
*/
|
||||
public void clear() {
|
||||
clearSize();
|
||||
firstNode = null;
|
||||
lastNode = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public E addBeforeFirst(final E elementIn) {
|
||||
DoubleLinkedListNode<E> oldFirstNode = getFirstNode();
|
||||
DoubleLinkedListNode<E> newFirstNode =
|
||||
new DoubleLinkedListNode<>(elementIn);
|
||||
DoubleLinkedListNode.link(newFirstNode, oldFirstNode);
|
||||
setFirstNode(newFirstNode);
|
||||
this.incrementSize();
|
||||
return elementIn;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public E addAfterLast(final E elementIn) {
|
||||
DoubleLinkedListNode<E> oldLastNode = getLastNode();
|
||||
DoubleLinkedListNode<E> newLastNode =
|
||||
new DoubleLinkedListNode<>(elementIn);
|
||||
|
||||
setLastNode(newLastNode);
|
||||
|
||||
if (isEmpty()) {
|
||||
setFirstNode(getLastNode());
|
||||
} else {
|
||||
DoubleLinkedListNode.link(oldLastNode, newLastNode);
|
||||
}
|
||||
|
||||
this.incrementSize();
|
||||
|
||||
return elementIn;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public E removeFirst() {
|
||||
if (isEmpty()) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
DoubleLinkedListNode<E> oldFirstNode = getFirstNode();
|
||||
DoubleLinkedListNode<E> newFirstNode = oldFirstNode.getNext();
|
||||
E element = oldFirstNode.getValue();
|
||||
if (size() == 1) {
|
||||
clear();
|
||||
return element;
|
||||
}
|
||||
setFirstNode(newFirstNode);
|
||||
this.incrementSize();
|
||||
|
||||
if (this.size() == 1) {
|
||||
setLastNode(getFirstNode());
|
||||
}
|
||||
newFirstNode.unlinkPrevious();
|
||||
return element;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public E removeLast() {
|
||||
if (isEmpty()) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
E element = getLastNode().getValue();
|
||||
|
||||
if (this.size() == 1) {
|
||||
clear();
|
||||
return element;
|
||||
}
|
||||
DoubleLinkedListNode<E> beforeLastNode = getLastNode().getPrevious();
|
||||
beforeLastNode.unlinkNext();
|
||||
setLastNode(beforeLastNode);
|
||||
this.decrementSize();
|
||||
|
||||
|
||||
return element;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public E getFirst() {
|
||||
if (isEmpty()) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
return firstNode.getValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public E getLast() {
|
||||
if (isEmpty()) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
return lastNode.getValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public E remove(final int index) {
|
||||
DoubleLinkedListNode<E> node = getNodeByIndex(index);
|
||||
if (node.equals(getFirstNode())) {
|
||||
return removeFirst();
|
||||
}
|
||||
if (node.equals(getLastNode())) {
|
||||
return removeLast();
|
||||
}
|
||||
DoubleLinkedListNode beforeNode = node.getPrevious();
|
||||
DoubleLinkedListNode.link(beforeNode, node.getNext());
|
||||
return node.getValue();
|
||||
}
|
||||
|
||||
private DoubleLinkedListNode<E> getNodeByIndex(final int indexIn) {
|
||||
if (isEmpty()) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
if (indexIn > (size() - 1)) {
|
||||
throw new CollectionException(
|
||||
"Index " + indexIn + " is out of bounds.");
|
||||
}
|
||||
int index = 0;
|
||||
DoubleLinkedListNode<E> node = getFirstNode();
|
||||
while (node != null) {
|
||||
if (index == indexIn) {
|
||||
return node;
|
||||
}
|
||||
node = node.getNext();
|
||||
index++;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/**
|
||||
* Returns the element at the specified position in this list.
|
||||
*
|
||||
* @param index starting from 1
|
||||
*
|
||||
* @return value
|
||||
*/
|
||||
public E get(final int index) {
|
||||
return getNodeByIndex(index).getValue();
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public E set(final int index, final E element) {
|
||||
getNodeByIndex(index).setValue(element);
|
||||
return element;
|
||||
}
|
||||
}
|
@ -0,0 +1,166 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.collections;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.nanoboot.powerframework.collections.internal.AbstractLinkedListNode;
|
||||
|
||||
/**
|
||||
* Represents one node of linked list.
|
||||
*
|
||||
* It is used to store a value based on the T.
|
||||
*
|
||||
* These nodes are linked (forward direction only).
|
||||
*
|
||||
* @param <E> The type of the value this node holds
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
public class DoubleLinkedListNode<E> extends AbstractLinkedListNode<E> {
|
||||
|
||||
/**
|
||||
* Adds the reference for the node2 to the node1. The node1 will be
|
||||
* connected to the node2
|
||||
*
|
||||
* @param node1 The node, which field next will be set to node2.
|
||||
* @param node2 The node, which will be set as the next node of the node1
|
||||
*/
|
||||
static void link(final DoubleLinkedListNode node1,
|
||||
final DoubleLinkedListNode node2) {
|
||||
if (node1 != null) {
|
||||
node1.setNext(node2);
|
||||
}
|
||||
if (node2 != null) {
|
||||
node2.setPrevious(node1);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Reference for the previous node.
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
private DoubleLinkedListNode<E> previous = null;
|
||||
|
||||
/**
|
||||
* Reference for the next node.
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
private DoubleLinkedListNode<E> next = null;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* Used to create node without key (Key is empty String), used in linked
|
||||
* list, which does not need keys.
|
||||
*
|
||||
* @param valueIn the appropriate field key will be set by this value
|
||||
*/
|
||||
DoubleLinkedListNode(final E valueIn) {
|
||||
this.setValue(valueIn);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param type type of the node to get
|
||||
* @return node by type
|
||||
*/
|
||||
public DoubleLinkedListNode<E> getNodeByType(
|
||||
final DoubleLinkedListNodeReferenceType type) {
|
||||
if (type.isPrevious()) {
|
||||
return previous;
|
||||
} else {
|
||||
return next;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param type type of the node to get
|
||||
* @param node to set
|
||||
*/
|
||||
public void setNodeByType(
|
||||
final DoubleLinkedListNodeReferenceType type,
|
||||
final DoubleLinkedListNode<E> node) {
|
||||
if (type.isPrevious()) {
|
||||
setPrevious(node);
|
||||
} else {
|
||||
setNext(node);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if this node has next node, otherwise false.
|
||||
*
|
||||
* @return boolean value
|
||||
*/
|
||||
boolean hasPrevious() {
|
||||
return has(DoubleLinkedListNodeReferenceType.PREVIOUS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if this node has next node, otherwise false.
|
||||
*
|
||||
* @return boolean value
|
||||
*/
|
||||
boolean hasNext() {
|
||||
return has(DoubleLinkedListNodeReferenceType.NEXT);
|
||||
}
|
||||
/**
|
||||
* Return true if this node has node with this type, otherwise false.
|
||||
* @param type type of the node to get
|
||||
* @return boolean value
|
||||
*/
|
||||
boolean has(final DoubleLinkedListNodeReferenceType type) {
|
||||
DoubleLinkedListNode<E> node = getNodeByType(type);
|
||||
return node != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the reference of the next node from this node.
|
||||
*/
|
||||
void unlinkAll() {
|
||||
unlinkPrevious();
|
||||
unlinkNext();
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the reference of the next node from this node.
|
||||
*/
|
||||
void unlinkPrevious() {
|
||||
unlink(DoubleLinkedListNodeReferenceType.PREVIOUS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the reference of the next node from this node.
|
||||
*/
|
||||
void unlinkNext() {
|
||||
unlink(DoubleLinkedListNodeReferenceType.NEXT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unlinks node.
|
||||
* @param type type of the node to unlink
|
||||
*/
|
||||
void unlink(final DoubleLinkedListNodeReferenceType type) {
|
||||
setNodeByType(type, null);
|
||||
}
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.collections;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
public enum DoubleLinkedListNodeReferenceType {
|
||||
/**
|
||||
* Previous node.
|
||||
*/
|
||||
PREVIOUS,
|
||||
/**
|
||||
* Next node.
|
||||
*/
|
||||
NEXT;
|
||||
|
||||
/**
|
||||
* Checks the type.
|
||||
* @return true, if it the previous type, otherwise false.
|
||||
*/
|
||||
public boolean isPrevious() {
|
||||
return this == PREVIOUS;
|
||||
}
|
||||
/**
|
||||
* Checks the type.
|
||||
* @return true, if it the next type, otherwise false.
|
||||
*/
|
||||
public boolean isNext() {
|
||||
return this == NEXT;
|
||||
}
|
||||
/**
|
||||
* Returns the opposite reference.
|
||||
* @return opposite reference
|
||||
*/
|
||||
public DoubleLinkedListNodeReferenceType opposite() {
|
||||
if (this.isPrevious()) {
|
||||
return NEXT;
|
||||
} else {
|
||||
return PREVIOUS;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.collections;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import org.nanoboot.powerframework.utils.StringUtils;
|
||||
|
||||
import java.util.Map;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
@AllArgsConstructor
|
||||
public class KeyValue<V> implements Map.Entry<String, V> {
|
||||
/**
|
||||
* Key used to identify this node, if node with this key is wanted.
|
||||
*/
|
||||
@Getter
|
||||
private String key = StringUtils.EMPTY_STRING;
|
||||
/**
|
||||
* The value this node is holding.
|
||||
*/
|
||||
@Getter
|
||||
private V value;
|
||||
/**
|
||||
* Setter for key.
|
||||
* @param k key
|
||||
* @return the new key
|
||||
*/
|
||||
public String setKey(final String k) {
|
||||
this.key = k;
|
||||
return this.key;
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public V setValue(final V v) {
|
||||
this.value = v;
|
||||
return this.value;
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return key + "=" + value;
|
||||
}
|
||||
}
|
@ -0,0 +1,215 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.collections;
|
||||
|
||||
import org.nanoboot.powerframework.core.PowerObject;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
/**
|
||||
* Represents map data structure. Values of type T are mapped to String and
|
||||
* searched by this String.
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
* @param <E> the type of items, this map will store.
|
||||
*/
|
||||
public class PowerCollection<E> extends PowerObject
|
||||
implements Collection<E> {
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* <p>
|
||||
* Constructor description
|
||||
*
|
||||
* @param items elements
|
||||
*/
|
||||
public PowerCollection(final E... items) {
|
||||
for (E e : items) {
|
||||
add(e);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* List, where are stored items of this map.
|
||||
*/
|
||||
private final SingleLinkedList<E> list = new SingleLinkedList<>();
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public int size() {
|
||||
return list.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return list.isEmpty();
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public boolean contains(final Object o) {
|
||||
return list.contains(o);
|
||||
}
|
||||
|
||||
private class CollectionIterator implements Iterator<E> {
|
||||
|
||||
/**
|
||||
* Internal list.
|
||||
*/
|
||||
private final SingleLinkedList<E> list;
|
||||
/**
|
||||
* Current index.
|
||||
*/
|
||||
private int currentIndex = 0;
|
||||
|
||||
CollectionIterator(final SingleLinkedList<E> listIn) {
|
||||
this.list = listIn;
|
||||
}
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return currentIndex < list.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() {
|
||||
if (!hasNext()) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
E result = list.get(currentIndex);
|
||||
currentIndex++;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Iterator<E> iterator() {
|
||||
return new CollectionIterator(this.list);
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Object[] toArray() {
|
||||
return this.list.toArray();
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public <T> T[] toArray(final T[] ts) {
|
||||
return (T[]) this.list.toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public boolean add(final E e) {
|
||||
return this.list.add(e);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public boolean remove(final Object o) {
|
||||
return this.list.remove(o);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public boolean containsAll(final Collection<?> collection) {
|
||||
for (Object e : collection) {
|
||||
if (!contains(e)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public boolean addAll(final Collection<? extends E> collection) {
|
||||
for (E e : collection) {
|
||||
this.add(e);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public boolean removeAll(final Collection<?> collection) {
|
||||
for (Object e: collection) {
|
||||
collection.remove(e);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public boolean retainAll(final Collection<?> collection) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void clear() {
|
||||
this.list.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns internal list.
|
||||
* @return internal list
|
||||
*/
|
||||
protected SingleLinkedList<E> getInternalList() {
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return list.toString();
|
||||
}
|
||||
protected final boolean throwUnsupportedOperationException() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
@ -0,0 +1,193 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.collections;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
public class PowerList<E> extends PowerCollection<E> implements List<E> {
|
||||
/**
|
||||
* Linked list, where are stored items of this queue.
|
||||
*/
|
||||
private final SingleLinkedList<E> list = getInternalList();
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* Constructs an empty list.
|
||||
*/
|
||||
public PowerList() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* Constructor description
|
||||
*
|
||||
* @param items elements
|
||||
*/
|
||||
public PowerList(final E... items) {
|
||||
super(items);
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public boolean addAll(
|
||||
final int i, final Collection<? extends E> collection) {
|
||||
return throwUnsupportedOperationException();
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public E get(final int i) {
|
||||
return list.get(i);
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public E set(final int i, final E e) {
|
||||
list.set(i, e);
|
||||
return e;
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void add(final int i, final E e) {
|
||||
throwUnsupportedOperationException();
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public E remove(final int i) {
|
||||
return list.remove(i);
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public int indexOf(final Object o) {
|
||||
throwUnsupportedOperationException();
|
||||
return 0;
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public int lastIndexOf(final Object o) {
|
||||
throwUnsupportedOperationException();
|
||||
return 0;
|
||||
}
|
||||
|
||||
private class PowerListIterator<E> implements ListIterator<E> {
|
||||
/**
|
||||
* Internal iterator.
|
||||
*/
|
||||
private final Iterator<E> iterator;
|
||||
|
||||
PowerListIterator(final Iterator<E> iteratorIn) {
|
||||
this.iterator = iteratorIn;
|
||||
}
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return iterator.hasNext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() {
|
||||
return iterator.next();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPrevious() {
|
||||
throwUnsupportedOperationException();
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E previous() {
|
||||
throwUnsupportedOperationException();
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int nextIndex() {
|
||||
throwUnsupportedOperationException();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int previousIndex() {
|
||||
throwUnsupportedOperationException();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
throwUnsupportedOperationException();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(final E e) {
|
||||
throwUnsupportedOperationException();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(final E e) {
|
||||
|
||||
}
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public ListIterator<E> listIterator() {
|
||||
return new PowerListIterator<>(this.iterator());
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public ListIterator<E> listIterator(final int i) {
|
||||
throwUnsupportedOperationException();
|
||||
return null;
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public List<E> subList(final int i, final int i1) {
|
||||
throwUnsupportedOperationException();
|
||||
return null;
|
||||
}
|
||||
}
|
@ -0,0 +1,266 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.collections;
|
||||
|
||||
import org.nanoboot.powerframework.core.PowerObject;
|
||||
import org.nanoboot.powerframework.core.exceptions.NotYetImplementedException;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Represents map data structure. Values of type T are mapped to String and
|
||||
* searched by this String.
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
* @param <T> the type of items, this map will store.
|
||||
*/
|
||||
public class PowerMap<T> extends PowerObject implements Map<String, T> {
|
||||
|
||||
/**
|
||||
* List, where are stored items of this map.
|
||||
*/
|
||||
private final SingleLinkedList<KeyValue<T>> list = new SingleLinkedList<>();
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public int size() {
|
||||
return list.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return list.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public boolean containsKey(final Object o) {
|
||||
for (KeyValue<T> e : list) {
|
||||
if (e.getKey().equals(o)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public boolean containsValue(final Object o) {
|
||||
for (KeyValue<T> e : list) {
|
||||
if (e.getValue().equals(o)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public T get(final Object o) {
|
||||
for (KeyValue<T> e : list) {
|
||||
if (e.getKey().equals(o)) {
|
||||
return e.getValue();
|
||||
}
|
||||
}
|
||||
throw new NoSuchElementException("There is no key " + o);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public T put(final String s, final T t) {
|
||||
this.list.add(new KeyValue(s, t));
|
||||
return t;
|
||||
}
|
||||
/**
|
||||
* Puts and return this.
|
||||
* @param s key
|
||||
* @param t value
|
||||
* @return this
|
||||
*/
|
||||
public PowerMap<T> putAndReturn(final String s, final T t) {
|
||||
put(s, t);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public T remove(final Object o) {
|
||||
for (KeyValue<T> e : list) {
|
||||
if (e.getKey().equals(o)) {
|
||||
this.list.remove(e);
|
||||
return e.getValue();
|
||||
}
|
||||
}
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void putAll(final Map<? extends String, ? extends T> map) {
|
||||
for (Entry<? extends String, ? extends T> entry : map.entrySet()) {
|
||||
String key = entry.getKey();
|
||||
T value = entry.getValue();
|
||||
put(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void clear() {
|
||||
this.list.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Set<String> keySet() {
|
||||
Set set = new HashSet<String>();
|
||||
for (Entry<? extends String, ? extends T> entry : this.entrySet()) {
|
||||
String key = entry.getKey();
|
||||
set.add(key);
|
||||
}
|
||||
return set;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public List<String> keyList() {
|
||||
List<String> list2 = new PowerList<>();
|
||||
for (KeyValue e : list) {
|
||||
String key = e.getKey();
|
||||
list2.add(key);
|
||||
}
|
||||
return list2;
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Collection<T> values() {
|
||||
throw new NotYetImplementedException("Not implemented.");
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Set<Entry<String, T>> entrySet() {
|
||||
Set<Entry<String, T>> set = new HashSet<>();
|
||||
Iterator<KeyValue<T>> iterator = this.list.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
set.add(iterator.next());
|
||||
}
|
||||
return set;
|
||||
}
|
||||
////
|
||||
/**
|
||||
* Iterator.
|
||||
* @return iterator
|
||||
*/
|
||||
public Iterator<KeyValue<T>> iterator() {
|
||||
return new MapIterator(this.list);
|
||||
}
|
||||
|
||||
private class MapIterator implements Iterator<KeyValue<T>> {
|
||||
|
||||
/**
|
||||
* Internal list.
|
||||
*/
|
||||
private final SingleLinkedList<KeyValue<T>> list;
|
||||
/**
|
||||
* Current index.
|
||||
*/
|
||||
private int currentIndex = 0;
|
||||
|
||||
MapIterator(final SingleLinkedList<KeyValue<T>> listIn) {
|
||||
this.list = listIn;
|
||||
}
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return currentIndex < list.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public KeyValue<T> next() {
|
||||
if (!hasNext()) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
KeyValue<T> result = list.get(currentIndex);
|
||||
currentIndex++;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the dictionary entry with the given key to the given value.
|
||||
*
|
||||
* @param keyIn key to search
|
||||
* @param valueIn value mapped to key will be updated to the valueIn
|
||||
*
|
||||
* @return this map
|
||||
*/
|
||||
public T replace(
|
||||
final String keyIn,
|
||||
final T valueIn) {
|
||||
this.remove(keyIn);
|
||||
this.put(keyIn, valueIn);
|
||||
return valueIn;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (KeyValue e: this.list) {
|
||||
sb.append(e.getKey()).append("=").append(e.getValue()).append("\n");
|
||||
}
|
||||
String result = sb.toString();
|
||||
return result.substring(0, result.length() - 1);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,87 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.collections;
|
||||
import java.util.Queue;
|
||||
|
||||
/**
|
||||
* Represents Queue- linear data structure.
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
* @param <E> object
|
||||
*/
|
||||
public class PowerQueue<E> extends PowerCollection<E> implements Queue<E> {
|
||||
|
||||
/**
|
||||
* Linked list, where are stored items of this queue.
|
||||
*/
|
||||
private final SingleLinkedList<E> list = getInternalList();
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public boolean offer(final E e) {
|
||||
return add(e);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public E remove() {
|
||||
return list.removeFirst();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public E poll() {
|
||||
return list.removeFirst();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public E element() {
|
||||
return list.getFirst();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public E peek() {
|
||||
return list.getFirst();
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds and returns.
|
||||
* @param item element to add
|
||||
* @return this
|
||||
*/
|
||||
public PowerQueue<E> addAndReturn(final E item) {
|
||||
add(item);
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,144 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.collections;
|
||||
|
||||
import org.nanoboot.powerframework.collections.arrays.ObjectArray;
|
||||
import org.nanoboot.powerframework.random.generators.RandomGenerator;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Here goes the description of this class.
|
||||
* <p>
|
||||
* Set is unordered collection of values and contains no duplicates.
|
||||
*
|
||||
* @param <E> element type
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
public class PowerSet<E> extends PowerCollection<E> implements Set<E> {
|
||||
/**
|
||||
* Linked list, where are stored items of this queue.
|
||||
*/
|
||||
private final SingleLinkedList<E> list = getInternalList();
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* <p>
|
||||
* Constructor description
|
||||
*
|
||||
* @param items elements
|
||||
*/
|
||||
public PowerSet(final E... items) {
|
||||
super(items);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public boolean add(final E item) {
|
||||
if (contains(item)) {
|
||||
String msg = "Can't add item " + item
|
||||
+ ". This object is already stored in this set.";
|
||||
throw new CollectionException(msg);
|
||||
}
|
||||
return this.list.add(item);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns random element of this Set.
|
||||
*
|
||||
* @return value
|
||||
*/
|
||||
public E getRandom() {
|
||||
int index = RandomGenerator.getDefaultImplStatic()
|
||||
.nextInt(1, list.size());
|
||||
return list.get(index);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return iterator for this list
|
||||
*/
|
||||
@Override
|
||||
public Iterator<E> iterator() {
|
||||
return new SetIterator(list);
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterator implementation.
|
||||
*/
|
||||
private class SetIterator implements Iterator<E> {
|
||||
|
||||
/**
|
||||
* Internal list.
|
||||
*/
|
||||
private final SingleLinkedList<E> list;
|
||||
/**
|
||||
* Indexes.
|
||||
*/
|
||||
private final SingleLinkedList<Integer> indexes;
|
||||
|
||||
SetIterator(final SingleLinkedList<E> listIn) {
|
||||
this.list = listIn;
|
||||
this.indexes = new SingleLinkedList<>();
|
||||
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
indexes.add(i);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return !indexes.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() {
|
||||
if (!hasNext()) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
int randomArrayListIndexesListIndex = RandomGenerator
|
||||
.getDefaultImplStatic().nextInt(0, indexes.size() - 1);
|
||||
int randomIndex = indexes.get(randomArrayListIndexesListIndex);
|
||||
E element = list.get(randomIndex);
|
||||
indexes.remove(randomArrayListIndexesListIndex);
|
||||
return element;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates object array.
|
||||
*
|
||||
* @return object array
|
||||
*/
|
||||
public final ObjectArray<E> toObjectArray() {
|
||||
ObjectArray<E> objectArray = new ObjectArray<>(this.size());
|
||||
int index = 1;
|
||||
for (E element : this) {
|
||||
objectArray.set(element, index++);
|
||||
}
|
||||
return objectArray;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.collections;
|
||||
|
||||
/**
|
||||
* Represents Stack- linear data structure.
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
* @param <E> object
|
||||
*/
|
||||
public class PowerStack<E> extends PowerCollection<E> implements StackI<E> {
|
||||
|
||||
/**
|
||||
* Linked list, where are stored items of this queue.
|
||||
*/
|
||||
private final SingleLinkedList<E> list = getInternalList();
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public E push(final E item) {
|
||||
list.addBeforeFirst(item);
|
||||
return item;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pushes and returns.
|
||||
* @param item element to push
|
||||
* @return this
|
||||
*/
|
||||
public PowerStack<E> pushAndReturn(final E item) {
|
||||
push(item);
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public E pop() {
|
||||
return list.removeFirst();
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public E peek() {
|
||||
return list.getFirst();
|
||||
}
|
||||
}
|
@ -0,0 +1,132 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.collections;
|
||||
|
||||
import lombok.Getter;
|
||||
import org.nanoboot.powerframework.core.PowerObject;
|
||||
import org.nanoboot.powerframework.utils.annotations.InProgress;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
@InProgress
|
||||
public class Properties extends PowerObject {
|
||||
/**
|
||||
* Constant #.
|
||||
*/
|
||||
private static final String COMMENT_START_CHARACTER = "#";
|
||||
/**
|
||||
* Constant =.
|
||||
*/
|
||||
private static final String EQUAL = "=";
|
||||
/**
|
||||
* Constant 0.
|
||||
*/
|
||||
private static final int PROPERTYPOSITION = 0;
|
||||
/**
|
||||
* Constant 1.
|
||||
*/
|
||||
private static final int VALUEPOSITION = 1;
|
||||
/**
|
||||
* Map mapping properties and their values.
|
||||
*/
|
||||
@Getter
|
||||
private PowerMap<String> map = new PowerMap<>();
|
||||
|
||||
/**
|
||||
* @param propertiesFileTextIn input text
|
||||
*/
|
||||
public Properties(final String propertiesFileTextIn) {
|
||||
String[] rows = propertiesFileTextIn.split("\\r?\\n");
|
||||
for (String row : rows) {
|
||||
if (!row.isEmpty() && !row.startsWith(COMMENT_START_CHARACTER)) {
|
||||
String[] array = getArrayFromRowIfPossible(row);
|
||||
String property = array[PROPERTYPOSITION];
|
||||
|
||||
if (array.length == 2) {
|
||||
String value = array[VALUEPOSITION];
|
||||
map.put(property, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Split row by first "=" occurrence.
|
||||
*
|
||||
* @param rowIn row
|
||||
* @return null, if there is no "="
|
||||
*/
|
||||
private String[] getArrayFromRowIfPossible(final String rowIn) {
|
||||
return rowIn.split(EQUAL, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param propertyIn property
|
||||
* @return
|
||||
*/
|
||||
public String getProperty(final String propertyIn) {
|
||||
return map.get(propertyIn);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param propertyIn property
|
||||
* @param valueIn value
|
||||
*/
|
||||
public void setProperty(final String propertyIn,
|
||||
final String valueIn) {
|
||||
map.replace(propertyIn, valueIn);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param propertyIn property
|
||||
* @return true, if the property is present, otherwise false
|
||||
*/
|
||||
public boolean hasProperty(final String propertyIn) {
|
||||
return map.containsKey(propertyIn);
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
if (this.getMap().isEmpty()) {
|
||||
return "";
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
String commaSpace = ", ";
|
||||
for (String key : this.getMap().keySet()) {
|
||||
sb
|
||||
.append(key)
|
||||
.append(EQUAL)
|
||||
.append(this.getMap().get(key))
|
||||
.append(commaSpace);
|
||||
}
|
||||
String result = sb.toString();
|
||||
return result.substring(0, result.length() - commaSpace.length());
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,442 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.collections;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.nanoboot.powerframework.collections.internal.AbstractLinkedList;
|
||||
import org.nanoboot.powerframework.core.exceptions.NotYetImplementedException;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
/**
|
||||
* Represents linked list data structure.
|
||||
*
|
||||
* @param <E> The type of the values stored in this list.
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
public class SingleLinkedList<E> extends AbstractLinkedList<E>
|
||||
implements Collection<E> {
|
||||
/**
|
||||
* The reference to the first node of this list.
|
||||
*/
|
||||
@Getter(AccessLevel.PRIVATE)
|
||||
@Setter(AccessLevel.PRIVATE)
|
||||
private SingleLinkedListNode<E> firstNode = null;
|
||||
/**
|
||||
* The reference to the last node of this list.
|
||||
*/
|
||||
@Getter(AccessLevel.PRIVATE)
|
||||
@Setter(AccessLevel.PRIVATE)
|
||||
private SingleLinkedListNode<E> lastNode = null;
|
||||
|
||||
/**
|
||||
* Iterator.
|
||||
*
|
||||
* @param <T> Node type
|
||||
*/
|
||||
private static class SingleLinkedListIterator<T>
|
||||
implements Iterator<T> {
|
||||
/**
|
||||
* Current node.
|
||||
*/
|
||||
@Getter(AccessLevel.PRIVATE)
|
||||
private SingleLinkedListNode<T> currentNode;
|
||||
|
||||
/**
|
||||
* @param node start node
|
||||
*/
|
||||
SingleLinkedListIterator(final SingleLinkedListNode<T> node) {
|
||||
this.currentNode = new SingleLinkedListNode<>(null);
|
||||
this.currentNode.setNext(node);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return currentNode.hasNext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public T next() {
|
||||
if (!hasNext()) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
this.currentNode = currentNode.getNext();
|
||||
return this.getCurrentNode().getValue();
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* Constructs an empty list.
|
||||
*/
|
||||
public SingleLinkedList() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* Constructor description
|
||||
*
|
||||
* @param items elements
|
||||
*/
|
||||
public SingleLinkedList(final E... items) {
|
||||
addAll(items);
|
||||
}
|
||||
////
|
||||
/**
|
||||
* Setter for name.
|
||||
*
|
||||
* @param items elements
|
||||
*/
|
||||
public void addAll(final E... items) {
|
||||
for (E element : items) {
|
||||
add(element);
|
||||
}
|
||||
}
|
||||
////
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public boolean contains(final Object o) {
|
||||
for (E e : this) {
|
||||
if (e.hashCode() == o.hashCode()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public final Iterator<E> iterator() {
|
||||
return new SingleLinkedListIterator(this.getFirstNode());
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Object[] toArray() {
|
||||
Object[] array = new Object[this.size()];
|
||||
int index = 0;
|
||||
for (E e : this) {
|
||||
array[index] = e;
|
||||
index++;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public <T1> T1[] toArray(final T1[] t1s) {
|
||||
throw new NotYetImplementedException("No detail");
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public boolean add(final E e) {
|
||||
this.addAfterLast(e);
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public boolean remove(final Object o) {
|
||||
int index = 0;
|
||||
SingleLinkedListNode<E> nodeToRemove;
|
||||
SingleLinkedListNode<E> beforeNode;
|
||||
for (E e : this) {
|
||||
if (e.hashCode() == o.hashCode()) {
|
||||
remove(index);
|
||||
return true;
|
||||
}
|
||||
index++;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public boolean containsAll(final Collection<?> collection) {
|
||||
if (collection.isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
for (Object e :collection) {
|
||||
if (!this.contains(e)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public boolean addAll(final Collection<? extends E> collection) {
|
||||
for (E e : collection) {
|
||||
this.add(e);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public boolean removeAll(final Collection<?> collection) {
|
||||
for (Object e : collection) {
|
||||
remove(e);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public boolean retainAll(final Collection<?> collection) {
|
||||
throw new NotYetImplementedException("No detail");
|
||||
}
|
||||
/**
|
||||
* Clears this list.
|
||||
* All elements will be removed.
|
||||
*/
|
||||
public void clear() {
|
||||
clearSize();
|
||||
firstNode = null;
|
||||
lastNode = null;
|
||||
}
|
||||
////
|
||||
/**
|
||||
* Adds new element before first element.
|
||||
*
|
||||
* @param elementIn element
|
||||
* @return this linked list
|
||||
*/
|
||||
public E addBeforeFirst(
|
||||
final E elementIn) {
|
||||
SingleLinkedListNode<E> oldRootNode = getFirstNode();
|
||||
SingleLinkedListNode<E> newRootNode =
|
||||
new SingleLinkedListNode<>(elementIn);
|
||||
SingleLinkedListNode.link(newRootNode, oldRootNode);
|
||||
setFirstNode(newRootNode);
|
||||
this.incrementSize();
|
||||
if (this.size() == 1) {
|
||||
setLastNode(getFirstNode());
|
||||
}
|
||||
return elementIn;
|
||||
}
|
||||
/**
|
||||
* Adds new element after last element.
|
||||
*
|
||||
* @param elementIn element
|
||||
* @return this linked list
|
||||
*/
|
||||
public E addAfterLast(final E elementIn) {
|
||||
|
||||
SingleLinkedListNode<E> oldLastNode = getLastNode();
|
||||
SingleLinkedListNode<E> newNode;
|
||||
newNode = new SingleLinkedListNode<>(elementIn);
|
||||
|
||||
if (isEmpty()) {
|
||||
setFirstNode(newNode);
|
||||
} else {
|
||||
SingleLinkedListNode.link(oldLastNode, newNode);
|
||||
}
|
||||
setLastNode(newNode);
|
||||
this.incrementSize();
|
||||
|
||||
return elementIn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes first element.
|
||||
*
|
||||
* @return removed element
|
||||
*/
|
||||
public E removeFirst() {
|
||||
if (isEmpty()) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
|
||||
SingleLinkedListNode<E> oldFirstNode = getFirstNode();
|
||||
SingleLinkedListNode<E> newFirstNode = oldFirstNode.getNext();
|
||||
E element = oldFirstNode.getValue();
|
||||
if (size() == 1) {
|
||||
clear();
|
||||
return element;
|
||||
}
|
||||
setFirstNode(newFirstNode);
|
||||
this.decrementSize();
|
||||
|
||||
if (this.size() == 1) {
|
||||
setLastNode(getFirstNode());
|
||||
}
|
||||
return element;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes last element.
|
||||
*
|
||||
* @return removed element
|
||||
*/
|
||||
public E removeLast() {
|
||||
if (isEmpty()) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
E element = getLastNode().getValue();
|
||||
|
||||
if (this.size() == 1) {
|
||||
clear();
|
||||
} else {
|
||||
SingleLinkedListNode<E> tempNode = getFirstNode();
|
||||
while (tempNode.getNext() != getLastNode()) {
|
||||
tempNode = tempNode.getNext();
|
||||
}
|
||||
SingleLinkedListNode<E> beforeLastNode = tempNode;
|
||||
beforeLastNode.unlinkNext();
|
||||
setLastNode(beforeLastNode);
|
||||
this.decrementSize();
|
||||
}
|
||||
|
||||
return element;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return first element
|
||||
*/
|
||||
public E getFirst() {
|
||||
if (isEmpty()) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
return getFirstNode().getValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return last element
|
||||
*/
|
||||
public E getLast() {
|
||||
if (isEmpty()) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
return getLastNode().getValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the element at the specified position in this list.
|
||||
*
|
||||
* @param index index param
|
||||
* @return the removed element
|
||||
*/
|
||||
public E remove(final int index) {
|
||||
SingleLinkedListNode<E> node = getNodeByIndex(index);
|
||||
if (node.equals(getFirstNode())) {
|
||||
return removeFirst();
|
||||
}
|
||||
if (node.equals(getLastNode())) {
|
||||
return removeLast();
|
||||
}
|
||||
SingleLinkedListNode<E> beforeNode = getNodeByIndex(index - 1);
|
||||
beforeNode.setNext(node.getNext());
|
||||
decrementSize();
|
||||
return node.getValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the element at the specified position in this list.
|
||||
*
|
||||
* @param index starting from 0
|
||||
*
|
||||
* @return value
|
||||
*/
|
||||
public E get(final int index) {
|
||||
return getNodeByIndex(index).getValue();
|
||||
}
|
||||
|
||||
private SingleLinkedListNode<E> getNodeByIndex(final int indexIn) {
|
||||
if (isEmpty()) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
if (indexIn > (size() - 1)) {
|
||||
throw new CollectionException(
|
||||
"Index " + indexIn + " is out of bounds.");
|
||||
}
|
||||
int index = 0;
|
||||
SingleLinkedListNode<E> node = getFirstNode();
|
||||
while (node != null) {
|
||||
if (index == indexIn) {
|
||||
return node;
|
||||
}
|
||||
node = node.getNext();
|
||||
index++;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces the element at the specified position
|
||||
* in this list with the specified element.
|
||||
*
|
||||
* @param index index param
|
||||
* @param element element param
|
||||
* @return the new value
|
||||
*/
|
||||
public E set(final int index, final E element) {
|
||||
SingleLinkedListNode<E> node = getNodeByIndex(index);
|
||||
E originalValue = node.getValue();
|
||||
node.setValue(element);
|
||||
return originalValue;
|
||||
}
|
||||
@Override
|
||||
public final String toString() {
|
||||
String commaSpace = ", ";
|
||||
return toString(commaSpace);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a String representation of this list using the delimiter.
|
||||
* @param delimiter String delimiter
|
||||
* @return string representation of the list
|
||||
*/
|
||||
public final String toString(final String delimiter) {
|
||||
if (isEmpty()) {
|
||||
return "";
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (E e : this) {
|
||||
sb.append(e);
|
||||
if (!delimiter.isEmpty()) {
|
||||
sb.append(delimiter);
|
||||
}
|
||||
}
|
||||
String result = sb.toString();
|
||||
result = result.substring(0, result.length() - delimiter.length());
|
||||
return result;
|
||||
}
|
||||
}
|
@ -0,0 +1,86 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.collections;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.nanoboot.powerframework.collections.internal.AbstractLinkedListNode;
|
||||
|
||||
/**
|
||||
* Represents one node of linked list.
|
||||
*
|
||||
* It is used to store a value based on the T.
|
||||
*
|
||||
* These nodes are linked (forward direction only).
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
* @param <E> The type of the value this node holds
|
||||
*/
|
||||
class SingleLinkedListNode<E> extends AbstractLinkedListNode<E> {
|
||||
/**
|
||||
* Adds the reference for the node2 to the node1. The node1 will be
|
||||
* connected to the node2
|
||||
*
|
||||
* @param node1 The node, which field next will be set to node2.
|
||||
* @param node2 The node, which will be set as the next node of the node1
|
||||
*/
|
||||
static void link(
|
||||
final SingleLinkedListNode node1,
|
||||
final SingleLinkedListNode node2) {
|
||||
node1.setNext(node2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reference for the next node.
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
private SingleLinkedListNode next = null;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* Used to create node without key (Key is empty String), used in linked
|
||||
* list, which does not need keys.
|
||||
*
|
||||
* @param valueIn the appropriate field key will be set by this value
|
||||
*/
|
||||
SingleLinkedListNode(final E valueIn) {
|
||||
this.setValue(valueIn);
|
||||
}
|
||||
/**
|
||||
* Return true if this node has next node, otherwise false.
|
||||
*
|
||||
* @return boolean value
|
||||
*/
|
||||
boolean hasNext() {
|
||||
return getNext() != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the reference of the next node from this node.
|
||||
*/
|
||||
void unlinkNext() {
|
||||
setNext(null);
|
||||
}
|
||||
|
||||
}
|
@ -5,7 +5,7 @@
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
@ -19,45 +19,36 @@
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.collections;
|
||||
|
||||
/**
|
||||
* Represents one dictionary entry.
|
||||
*
|
||||
*
|
||||
* @author Robert Vokac e-mail: robertvokac@nanoboot.org
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
class DictionaryNode<T> {
|
||||
public interface StackI<E> {
|
||||
|
||||
private String key;
|
||||
private T element;
|
||||
private DictionaryNode next = null;
|
||||
/**
|
||||
* Pushes new element.
|
||||
* @param item element to add
|
||||
* @return element to add
|
||||
*/
|
||||
E push(E item);
|
||||
|
||||
DictionaryNode(String key, T element) {
|
||||
this.key = key;
|
||||
this.element = element;
|
||||
}
|
||||
|
||||
T getElement() {
|
||||
return element;
|
||||
}
|
||||
|
||||
void setElement(T element) {
|
||||
this.element = element;
|
||||
}
|
||||
|
||||
DictionaryNode getNext() {
|
||||
return next;
|
||||
}
|
||||
|
||||
void setNext(DictionaryNode next) {
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
void setKey(String key) {
|
||||
this.key = key;
|
||||
}
|
||||
/**
|
||||
* Pops element.
|
||||
* Remove first element.
|
||||
* @return element to remove
|
||||
*/
|
||||
E pop();
|
||||
/**
|
||||
* Returns the top element of this stack without removing this element.
|
||||
* @return the top element of this stack without removing this element
|
||||
*/
|
||||
E peek();
|
||||
|
||||
/**
|
||||
* Checks, if the stack is empty.
|
||||
* @return true, if this stack is empty, otherwise false
|
||||
*/
|
||||
boolean isEmpty();
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.collections;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.nanoboot.powerframework.core.PowerObject;
|
||||
import org.nanoboot.powerframework.utils.annotations.ToRemove;
|
||||
|
||||
/**
|
||||
* Here goes the description of this class.
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*
|
||||
*/
|
||||
@ToRemove
|
||||
public class Table extends PowerObject {
|
||||
/**
|
||||
* Name of the table.
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
private String name;
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* Not meant to be instantiated.
|
||||
*/
|
||||
private Table() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* Constructor description
|
||||
*
|
||||
* @param nameIn name
|
||||
*/
|
||||
public Table(final String nameIn) {
|
||||
this.setName(nameIn);
|
||||
}
|
||||
|
||||
}
|
@ -5,7 +5,7 @@
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
@ -18,31 +18,39 @@
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.pseudorandom;
|
||||
package org.nanoboot.powerframework.collections;
|
||||
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* Represents seed. Uses linear congruential function.
|
||||
*
|
||||
* @author Robert Vokac e-mail: robertvokac@nanoboot.org
|
||||
* @param <T> element type
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
class Seed {
|
||||
public class Tree<T> {
|
||||
/**
|
||||
* Root.
|
||||
*/
|
||||
@Getter
|
||||
private final TreeNode<T> root;
|
||||
|
||||
private long currentSeed;
|
||||
private static final int A = 16807;
|
||||
private static final int C = 0;
|
||||
private static final long M = 2147483647;
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public Tree() {
|
||||
root = new TreeNode<>();
|
||||
root.setName("root");
|
||||
|
||||
Seed(long seed) {
|
||||
this.currentSeed = seed;
|
||||
}
|
||||
/**
|
||||
* Constructor.
|
||||
* @param rootIn root node
|
||||
*/
|
||||
public Tree(final TreeNode<T> rootIn) {
|
||||
this.root = rootIn;
|
||||
}
|
||||
|
||||
long getNextNumber() {
|
||||
long number = ((A * currentSeed) + C) % M;
|
||||
this.currentSeed = number;
|
||||
return number;
|
||||
}
|
||||
|
||||
void jump() {
|
||||
getNextNumber();
|
||||
}
|
||||
}
|
@ -0,0 +1,105 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.collections;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.nanoboot.powerframework.collections.internal.AbstractTreeNode;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
* @param <T>
|
||||
*/
|
||||
public final class TreeNode<T> extends AbstractTreeNode<T> {
|
||||
/**
|
||||
* Children nodes.
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
private PowerList<TreeNode<T>> children = new PowerList<>();
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private boolean ableToHaveChildren = true;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public TreeNode() {
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param valueIn the value
|
||||
*/
|
||||
public TreeNode(final T valueIn) {
|
||||
this.setValue(valueIn);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param valueIn value
|
||||
* @param childIn child
|
||||
*/
|
||||
public TreeNode(final T valueIn, final TreeNode<T> childIn) {
|
||||
this.setValue(valueIn);
|
||||
addChild(childIn);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param valueIn value
|
||||
* @param childrenIn children
|
||||
*/
|
||||
public TreeNode(final T valueIn, final TreeNode<T>... childrenIn) {
|
||||
this.setValue(valueIn);
|
||||
addChildren(childrenIn);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param childIn child to add
|
||||
*/
|
||||
public void addChild(final TreeNode<T> childIn) {
|
||||
getChildren().add(childIn);
|
||||
childIn.setParent(this);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param childrenIn children to add
|
||||
*/
|
||||
public void addChildren(final TreeNode<T>... childrenIn) {
|
||||
for (TreeNode<T> element : childrenIn) {
|
||||
addChild(element);
|
||||
}
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @return true, if this node has some children, otherwise false
|
||||
*/
|
||||
public boolean hasChildren() {
|
||||
return !getChildren().isEmpty();
|
||||
}
|
||||
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,38 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.collections.arrays;
|
||||
|
||||
import org.nanoboot.powerframework.collections.CollectionException;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
public class ArrayException extends CollectionException {
|
||||
|
||||
public ArrayException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.collections.arrays;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
public enum ArrayType {
|
||||
//TODO CHAR- char array
|
||||
|
||||
BOOLEAN,
|
||||
BYTE,
|
||||
CHAR,
|
||||
SHORT,
|
||||
INT,
|
||||
LONG,
|
||||
FLOAT,
|
||||
DOUBLE,
|
||||
OBJECT;
|
||||
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.collections.arrays;
|
||||
|
||||
/**
|
||||
* There will be the choice to select the starting index:0 will be default
|
||||
* (maybe), but 1 or something else will be possible.
|
||||
*
|
||||
* @param <CLASS>
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
public class ByteArray {
|
||||
|
||||
private Array array;
|
||||
|
||||
public ByteArray(int... lengthIn) {
|
||||
array = new Array<Object>(ArrayType.INT, lengthIn);
|
||||
}
|
||||
|
||||
public ByteArray(byte... values) {
|
||||
array = new Array(values);
|
||||
}
|
||||
|
||||
public static ByteArray ofValues(byte... values) {
|
||||
ByteArray intArray = new ByteArray(values.length);
|
||||
intArray.array = new Array(values);
|
||||
return intArray;
|
||||
}
|
||||
|
||||
public int get(int... index) {
|
||||
return array.getInt(index);
|
||||
}
|
||||
|
||||
public void set(int value,
|
||||
int... index) {
|
||||
array.setInt(value, index);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param dimension 1 or 2 or 3 or 4
|
||||
* @return
|
||||
*/
|
||||
public int getLength(int dimension) {
|
||||
return this.array.getLength(dimension);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,100 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.collections.arrays;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* There will be the choice to select the starting index:0 will be default
|
||||
* (maybe), but 1 or something else will be possible.
|
||||
*
|
||||
* @param <CLASS>
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
public class IntArray implements Iterable<Integer> {
|
||||
|
||||
private Array array;
|
||||
|
||||
public IntArray(int... lengthIn) {
|
||||
array = new Array<Object>(ArrayType.INT, lengthIn);
|
||||
}
|
||||
|
||||
public static IntArray ofValues(int... values) {
|
||||
IntArray intArray = new IntArray(values.length);
|
||||
intArray.array = new Array(values);
|
||||
return intArray;
|
||||
}
|
||||
|
||||
public int get(int... index) {
|
||||
return array.getInt(index);
|
||||
}
|
||||
|
||||
public void set(int value,
|
||||
int... index) {
|
||||
array.setInt(value, index);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param dimension 1 or 2 or 3 or 4
|
||||
* @return
|
||||
*/
|
||||
public int getLength(int dimension) {
|
||||
return this.array.getLength(dimension);
|
||||
}
|
||||
|
||||
@java.lang.Override
|
||||
public Iterator<Integer> iterator() {
|
||||
if (array.getDimensions() != 1) {
|
||||
throw new ArrayException("For iterating is only supported for one-dimensional arrays.");
|
||||
}
|
||||
return new IntArrayIterator(array);
|
||||
}
|
||||
|
||||
class IntArrayIterator implements Iterator<Integer> {
|
||||
|
||||
private Array array;
|
||||
private int index = 0;
|
||||
|
||||
public IntArrayIterator(Array arrayIn) {
|
||||
this.array = arrayIn;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return (index + 1) <= array.getLength();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer next() {
|
||||
throwExceptionIfNoNextElement();
|
||||
return array.getInt(++index);
|
||||
}
|
||||
|
||||
private void throwExceptionIfNoNextElement() throws ArrayException {
|
||||
if (!hasNext()) {
|
||||
throw new ArrayException("There is no next element.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.collections.arrays;
|
||||
|
||||
/**
|
||||
* There will be the choice to select the starting index:0 will be default
|
||||
* (maybe), but 1 or something else will be possible.
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
* @param <C>
|
||||
*/
|
||||
public class ObjectArray<C> {
|
||||
|
||||
private Array<C> array;
|
||||
|
||||
public ObjectArray(int... lengthIn) {
|
||||
array = new Array<C>(ArrayType.OBJECT, lengthIn);
|
||||
}
|
||||
|
||||
public static <C> ObjectArray ofValues(C... values) {
|
||||
ObjectArray<C> objectArray = new ObjectArray<>(values.length);
|
||||
int index = 1;
|
||||
for (C element : values) {
|
||||
objectArray.set(element, index++);
|
||||
}
|
||||
return objectArray;
|
||||
|
||||
}
|
||||
// public ObjectArray(C... values) {
|
||||
// array = new Array<C>(values);
|
||||
// }
|
||||
|
||||
public C get(int... index) {
|
||||
return array.getObject(index);
|
||||
}
|
||||
|
||||
public void set(C value,
|
||||
int... index) {
|
||||
array.setObject(value, index);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param dimension 1 or 2 or 3 or 4
|
||||
* @return
|
||||
*/
|
||||
public int getLength(int dimension) {
|
||||
return this.array.getLength(dimension);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return
|
||||
*/
|
||||
public int getLength() {
|
||||
return this.array.getLength();
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* Classes for arrays (index starting from 1, not 0).
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
package org.nanoboot.powerframework.collections.arrays;
|
@ -0,0 +1,64 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.collections.buffer;
|
||||
|
||||
/**
|
||||
* Here goes the description of this class.
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
public class CharBuffer {
|
||||
|
||||
public int size() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public void putAtTheEnd(char ch) {
|
||||
|
||||
}
|
||||
|
||||
public void putAtTheEnd(CharBuffer charBuffer) {
|
||||
|
||||
}
|
||||
@Deprecated
|
||||
public void putCurrentCharAtTheEnd(CharBuffer charBuffer) {
|
||||
|
||||
}
|
||||
|
||||
public char get() {
|
||||
return ' ';
|
||||
}
|
||||
|
||||
public char get(int position) {
|
||||
return ' ';
|
||||
}
|
||||
|
||||
public void move(int move) {
|
||||
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "";
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,171 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.collections.internal;
|
||||
|
||||
import org.nanoboot.powerframework.collections.arrays.ObjectArray;
|
||||
|
||||
/**
|
||||
* Represents linked list data structure.
|
||||
* @param <E> element type
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
public abstract class AbstractLinkedList<E> implements Iterable<E> {
|
||||
/**
|
||||
* Size of this list.
|
||||
*/
|
||||
private int size = 0;
|
||||
|
||||
/**
|
||||
* Returns size of this list.
|
||||
* @return size of this list
|
||||
*/
|
||||
public final int size() {
|
||||
return this.size;
|
||||
}
|
||||
/**
|
||||
* Checks, if this list is empty.
|
||||
* @return true, if this list is empty, otherwise false
|
||||
*/
|
||||
public boolean isEmpty() {
|
||||
return this.size() == 0;
|
||||
}
|
||||
/**
|
||||
* Deletes all elements of this linked list.
|
||||
*/
|
||||
public abstract void clear();
|
||||
/**
|
||||
* Increments the size of this list by one.
|
||||
*/
|
||||
protected void incrementSize() {
|
||||
++size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrements the size of this list by one.
|
||||
*/
|
||||
protected void decrementSize() {
|
||||
--size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the size to 0.
|
||||
*/
|
||||
protected void clearSize() {
|
||||
size = 0;
|
||||
}
|
||||
/**
|
||||
* Adds new element before first element.
|
||||
*
|
||||
* @param elementIn element
|
||||
* @return this linked list
|
||||
*/
|
||||
public abstract E addBeforeFirst(E elementIn);
|
||||
|
||||
/**
|
||||
* Adds new element after last element.
|
||||
*
|
||||
* @param elementIn element
|
||||
* @return this linked list
|
||||
*/
|
||||
public abstract E addAfterLast(E elementIn);
|
||||
/**
|
||||
* Removes first element.
|
||||
*
|
||||
* @return removed element
|
||||
*/
|
||||
public abstract E removeFirst();
|
||||
|
||||
/**
|
||||
* Removes last element.
|
||||
*
|
||||
* @return removed element
|
||||
*/
|
||||
public abstract E removeLast();
|
||||
/**
|
||||
* @return first element
|
||||
*/
|
||||
public abstract E getFirst();
|
||||
|
||||
/**
|
||||
* @return last element
|
||||
*/
|
||||
public abstract E getLast();
|
||||
|
||||
/**
|
||||
* Removes the element at the specified position in this list.
|
||||
*
|
||||
* @param index index param
|
||||
* @return the removed element
|
||||
*/
|
||||
public abstract E remove(int index);
|
||||
|
||||
/**
|
||||
* Returns the element at the specified position in this list.
|
||||
*
|
||||
* @param index starting from 1
|
||||
*
|
||||
* @return value
|
||||
*/
|
||||
public abstract E get(int index);
|
||||
|
||||
/**
|
||||
* Replaces the element at the specified position
|
||||
* in this list with the specified element.
|
||||
*
|
||||
* @param index index param
|
||||
* @param element element param
|
||||
* @return the new value
|
||||
*/
|
||||
public abstract E set(int index, E element);
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
int index = 0;
|
||||
int lastIndex = this.size() - 1;
|
||||
for (E element : this) {
|
||||
stringBuilder.append(element.toString());
|
||||
if (index < lastIndex) {
|
||||
stringBuilder.append(", ");
|
||||
}
|
||||
++index;
|
||||
}
|
||||
return stringBuilder.toString();
|
||||
}
|
||||
/**
|
||||
* Converts this list to an ObjectArray.
|
||||
* @return ObjectArray
|
||||
*/
|
||||
public ObjectArray<E> toObjectArray() {
|
||||
ObjectArray<E> objectArray = new ObjectArray<>(this.size());
|
||||
int index = 1;
|
||||
for (E element : this) {
|
||||
objectArray.set(element, index++);
|
||||
}
|
||||
return objectArray;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.collections.internal;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
/**
|
||||
* Represents one node of linked list.
|
||||
*
|
||||
* It is used to store a value based on the T.
|
||||
*
|
||||
* These nodes are linked (forward direction only).
|
||||
*
|
||||
* @param <E> The type of the value this node holds
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
public abstract class AbstractLinkedListNode<E> {
|
||||
/**
|
||||
* Value.
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
private E value;
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.collections.internal;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param <T>
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
public abstract class AbstractTreeNode<T> {
|
||||
/**
|
||||
* Parent node.
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
private AbstractTreeNode<T> parent = null;
|
||||
/**
|
||||
* The value to be hold.
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
private T value;
|
||||
/**
|
||||
* Name of the node.
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
private String name;
|
||||
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* Internal classes.
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
package org.nanoboot.powerframework.collections.internal;
|
@ -0,0 +1,27 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* Collections used to store instances of classes.
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
package org.nanoboot.powerframework.collections;
|
0
power-collections/src/main/resources/.gitkeep
Normal file
0
power-collections/src/main/resources/.gitkeep
Normal file
@ -0,0 +1,412 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.collections;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.*;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
public class PowerMapTest {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public PowerMapTest() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of size method, of class map.
|
||||
*/
|
||||
@Test
|
||||
public void testsize() {
|
||||
//arrange
|
||||
PowerMap<String> map = new PowerMap<>();
|
||||
int expectedValue = 0;
|
||||
int returnedValue;
|
||||
//act
|
||||
returnedValue = map.size();
|
||||
//assert
|
||||
assertEquals(expectedValue, returnedValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of size method, of class map.
|
||||
*/
|
||||
@Test
|
||||
public void testsize2() {
|
||||
//arrange
|
||||
PowerMap<String> map = new PowerMap<>();
|
||||
int expectedValue = 0;
|
||||
int returnedValue;
|
||||
//act
|
||||
map.put("name", "John");
|
||||
map.put("surname", "Black");
|
||||
map.remove("name");
|
||||
map.remove("surname");
|
||||
returnedValue = map.size();
|
||||
//assert
|
||||
assertEquals(expectedValue, returnedValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of size method, of class map.
|
||||
*/
|
||||
@Test
|
||||
public void testsize3() {
|
||||
//arrange
|
||||
PowerMap<String> map = new PowerMap<>();
|
||||
int expectedValue = 2;
|
||||
int returnedValue;
|
||||
//act
|
||||
map.put("name", "John");
|
||||
map.put("surname", "Black");
|
||||
map.put("nick", "Johny");
|
||||
map.remove("surname");
|
||||
returnedValue = map.size();
|
||||
//assert
|
||||
assertEquals(expectedValue, returnedValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of size method, of class map.
|
||||
*/
|
||||
@Test
|
||||
public void testsize4() {
|
||||
//arrange
|
||||
PowerMap<String> map = new PowerMap<>();
|
||||
int expectedValue = 1;
|
||||
int returnedValue;
|
||||
//act
|
||||
map.put("surname", "Black");
|
||||
returnedValue = map.size();
|
||||
//assert
|
||||
assertEquals(expectedValue, returnedValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of isEmpty method, of class map.
|
||||
*/
|
||||
@Test
|
||||
public void testIsEmpty() {
|
||||
//arrange
|
||||
PowerMap<String> map = new PowerMap<>();
|
||||
boolean returnedValue;
|
||||
//act
|
||||
returnedValue = map.isEmpty();
|
||||
//assert
|
||||
assertTrue(returnedValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of isEmpty method, of class map.
|
||||
*/
|
||||
@Test
|
||||
public void testIsEmpty2() {
|
||||
//arrange
|
||||
PowerMap<String> map = new PowerMap<>();
|
||||
boolean returnedValue;
|
||||
//act
|
||||
map.put("name", "John");
|
||||
map.put("surname", "Black");
|
||||
map.remove("name");
|
||||
map.remove("surname");
|
||||
returnedValue = map.isEmpty();
|
||||
//assert
|
||||
assertTrue(returnedValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of isEmpty method, of class map.
|
||||
*/
|
||||
@Test
|
||||
public void testIsEmpty3() {
|
||||
//arrange
|
||||
PowerMap<String> map = new PowerMap<>();
|
||||
boolean returnedValue;
|
||||
//act
|
||||
map.put("name", "John");
|
||||
map.put("surname", "Black");
|
||||
map.put("nick", "Johny");
|
||||
map.remove("surname");
|
||||
returnedValue = map.isEmpty();
|
||||
//assert
|
||||
assertFalse(returnedValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of getValue method, of class map.
|
||||
*/
|
||||
@Test
|
||||
public void testIsEmpty4() {
|
||||
//arrange
|
||||
PowerMap<String> map = new PowerMap<>();
|
||||
boolean returnedValue;
|
||||
//act
|
||||
map.put("name", "John");
|
||||
map.put("surname", "Black");
|
||||
map.put("nick", "Johny");
|
||||
map.remove("name");
|
||||
returnedValue = map.isEmpty();
|
||||
//assert
|
||||
assertFalse(returnedValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of containsKey method, of class map.
|
||||
*/
|
||||
@Test
|
||||
public void containsKeyKey() {
|
||||
//arrange
|
||||
PowerMap<String> map = new PowerMap<>();
|
||||
boolean expectedValue = true;
|
||||
boolean returnedValue;
|
||||
//act
|
||||
map.put("name", "John");
|
||||
map.put("surname", "Black");
|
||||
map.put("nick", "Johny");
|
||||
returnedValue = map.containsKey("surname");
|
||||
//assert
|
||||
assertEquals(expectedValue, returnedValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of containsKey method, of class map.
|
||||
*/
|
||||
@Test
|
||||
public void containsKey2() {
|
||||
//arrange
|
||||
PowerMap<String> map = new PowerMap<>();
|
||||
boolean expectedValue = false;
|
||||
boolean returnedValue;
|
||||
//act
|
||||
map.put("name", "John");
|
||||
map.put("surname", "Black");
|
||||
map.put("nick", "Johny");
|
||||
returnedValue = map.containsKey("job");
|
||||
//assert
|
||||
assertEquals(expectedValue, returnedValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of containsKey method, of class map.
|
||||
*/
|
||||
@Test
|
||||
public void containsKey3() {
|
||||
//arrange
|
||||
PowerMap<String> map = new PowerMap<>();
|
||||
boolean expectedValue = true;
|
||||
boolean returnedValue;
|
||||
//act
|
||||
map.put("name", "John");
|
||||
returnedValue = map.containsKey("name");
|
||||
//assert
|
||||
assertEquals(expectedValue, returnedValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of add method, of class map.
|
||||
*/
|
||||
@Test
|
||||
public void testPut() {
|
||||
//arrange
|
||||
PowerMap<String> map = new PowerMap<>();
|
||||
String expectedString = "Johny";
|
||||
String returnedString;
|
||||
//act
|
||||
map.put("name", "John");
|
||||
map.put("surname", "Black");
|
||||
map.put("nick", "Johny");
|
||||
map.remove("surname");
|
||||
returnedString = map.get("nick");
|
||||
//assert
|
||||
assertEquals(expectedString, returnedString);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of add method, of class map.
|
||||
*/
|
||||
@Test
|
||||
public void testPut2() {
|
||||
//arrang
|
||||
PowerMap<String> map = new PowerMap<>();
|
||||
String expectedString = "Johny";
|
||||
String returnedString;
|
||||
//act
|
||||
map.put("name", "John");
|
||||
map.put("surname", "Black");
|
||||
map.put("nick", "Johny");
|
||||
map.remove("name");
|
||||
returnedString = map.get("nick");
|
||||
//assert
|
||||
assertEquals(expectedString, returnedString);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of add method, of class map.
|
||||
*/
|
||||
@Test
|
||||
public void testPut3() {
|
||||
//arrange
|
||||
PowerMap<String> map = new PowerMap<>();
|
||||
String expectedString = "Black";
|
||||
String returnedString;
|
||||
//act
|
||||
map.put("name", "John");
|
||||
map.put("surname", "Black");
|
||||
map.put("nick", "Johny");
|
||||
map.remove("nick");
|
||||
returnedString = map.get("surname");
|
||||
//assert
|
||||
assertEquals(expectedString, returnedString);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of getValue method, of class map.
|
||||
*/
|
||||
@Test
|
||||
public void testGet() {
|
||||
//arrange
|
||||
PowerMap<String> map = new PowerMap<>();
|
||||
String expectedString = "Black";
|
||||
String returnedString;
|
||||
//act
|
||||
map.put("name", "John");
|
||||
map.put("surname", "Black");
|
||||
map.put("nick", "Johny");
|
||||
map.remove("name");
|
||||
returnedString = map.get("surname");
|
||||
//assert
|
||||
assertEquals(expectedString, returnedString);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of getValue method, of class map.
|
||||
*/
|
||||
@Test
|
||||
public void testGet2() {
|
||||
//arrange
|
||||
PowerMap<String> map = new PowerMap<>();
|
||||
String expectedString = "Johny";
|
||||
String returnedString;
|
||||
//act
|
||||
map.put("nick", "Johny");
|
||||
returnedString = map.get("nick");
|
||||
//assert
|
||||
assertEquals(expectedString, returnedString);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of updateValue method, of class map.
|
||||
*/
|
||||
@Test
|
||||
public void testReplaceValue() {
|
||||
//arrange
|
||||
PowerMap<String> map = new PowerMap<>();
|
||||
String expectedString = "Blue";
|
||||
String returnedString;
|
||||
//act
|
||||
map.put("name", "John");
|
||||
map.put("surname", "Black");
|
||||
map.put("nick", "Johny");
|
||||
map.replace("surname", "Blue");
|
||||
returnedString = map.get("surname");
|
||||
//assert
|
||||
assertEquals(expectedString, returnedString);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of remove method, of class map.
|
||||
*/
|
||||
@Test
|
||||
public void testRemove() {
|
||||
//arrange
|
||||
boolean isExceptionThrown = false;
|
||||
PowerMap<String> map = new PowerMap<>();
|
||||
String expectedString = "Black";
|
||||
String returnedString;
|
||||
map.put("name", "John");
|
||||
map.put("surname", "Black");
|
||||
map.put("nick", "Johny");
|
||||
map.remove("surname");
|
||||
//act
|
||||
try {
|
||||
returnedString = map.get("surname");
|
||||
} catch (NoSuchElementException e) {
|
||||
isExceptionThrown = true;
|
||||
}
|
||||
//assert
|
||||
if (!isExceptionThrown) {
|
||||
fail("There should be thrown NoSuchElementException");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of getKeyIterator method, of class map.
|
||||
*/
|
||||
@Test
|
||||
public void testIterator() {
|
||||
//arrange
|
||||
PowerMap<String> map = new PowerMap<>();
|
||||
Iterator<KeyValue<String>> mapIterator;
|
||||
String expectedString = "Johny";
|
||||
String returnedString;
|
||||
String secondKey;
|
||||
map.put("name", "John");
|
||||
map.put("surname", "Black");
|
||||
map.put("nick", "Johny");
|
||||
map.remove("surname");
|
||||
mapIterator = map.iterator();
|
||||
//act
|
||||
mapIterator.next();
|
||||
secondKey = mapIterator.next().getKey();
|
||||
returnedString = map.get(secondKey);
|
||||
//assert
|
||||
assertEquals(expectedString, returnedString);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of toString method, of class map.
|
||||
*/
|
||||
@Test
|
||||
public void testToString() {
|
||||
//arrange
|
||||
PowerMap<String> map = new PowerMap<>();
|
||||
String expectedString = "name=John\nsurname=Black\nnick=Johny";
|
||||
String returnedString;
|
||||
//act
|
||||
map.put("name", "John");
|
||||
map.put("surname", "Black");
|
||||
map.put("nick", "Johny");
|
||||
returnedString = map.toString();
|
||||
//assert
|
||||
assertEquals(expectedString, returnedString);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,120 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.collections;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.*;
|
||||
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
public class PropertiesTest {
|
||||
private Properties properties;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
properties=new Properties(PROPERTIES_FILE_TEXT_IN);
|
||||
}
|
||||
public static final String PROPERTIES_FILE_TEXT_IN = "#Fri Jan 17 22:37:45 MYT 2014\n" +
|
||||
"dbpassword=password\n" +
|
||||
"database=localhost\n" +
|
||||
"dbuser=mkyong";
|
||||
@Test
|
||||
public void getProperty() {
|
||||
//prepare
|
||||
//execute
|
||||
//assert
|
||||
assertEquals(properties.getProperty("dbpassword"), "password");
|
||||
assertEquals(properties.getProperty("database"), "localhost");
|
||||
assertEquals(properties.getProperty("dbuser"), "mkyong");
|
||||
try {
|
||||
properties.getProperty("abc");
|
||||
throw new AssertionError("NoSuchElementException expected, but not thrown.");
|
||||
} catch (NoSuchElementException e) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setProperty() {
|
||||
//prepare
|
||||
//execute
|
||||
properties.setProperty("dbpassword", "newPassword");
|
||||
//assert
|
||||
assertEquals(properties.getProperty("dbpassword"), "newPassword");
|
||||
assertEquals(properties.getProperty("database"), "localhost");
|
||||
assertEquals(properties.getProperty("dbuser"), "mkyong");
|
||||
try {
|
||||
properties.getProperty("abc");
|
||||
throw new AssertionError("NoSuchElementException expected, but not thrown.");
|
||||
} catch (NoSuchElementException e) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hasProperty() {
|
||||
|
||||
//prepare
|
||||
//execute
|
||||
properties.setProperty("dbpassword", "newPassword");
|
||||
//assert
|
||||
assertTrue(properties.hasProperty("dbpassword"));
|
||||
assertTrue(properties.hasProperty("database"));
|
||||
assertTrue(properties.hasProperty("dbuser"));
|
||||
try {
|
||||
properties.getProperty("abc");
|
||||
throw new AssertionError("NoSuchElementException expected, but not thrown.");
|
||||
} catch (NoSuchElementException e) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToString() {
|
||||
//prepare
|
||||
String returned;
|
||||
//execute
|
||||
returned = properties.toString();
|
||||
//assert
|
||||
assertTrue(returned.contains("dbpassword=password"));
|
||||
assertTrue(returned.contains("database=localhost"));
|
||||
assertTrue(returned.contains("dbuser=mkyong"));
|
||||
assertTrue(returned.contains(", "));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getMap() {
|
||||
PowerMap<String> map = properties.getMap();
|
||||
assertNotNull(map);
|
||||
assertTrue(map.containsKey("dbpassword"));
|
||||
assertTrue(map.containsKey("database"));
|
||||
assertTrue(map.containsKey("dbuser"));
|
||||
}
|
||||
}
|
@ -5,7 +5,7 @@
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
@ -20,14 +20,19 @@
|
||||
|
||||
package org.nanoboot.powerframework.collections;
|
||||
|
||||
import org.nanoboot.powerframework.PowerRuntimeException;
|
||||
import org.junit.Test;
|
||||
import java.util.*;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.*;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author Robert Vokac e-mail: robertvokac@nanoboot.org
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
public class QueueTest {
|
||||
|
||||
/**
|
||||
@ -37,102 +42,102 @@ public class QueueTest {
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of getCountOfItems method, of class Queue.
|
||||
* Test of size method, of class Queue.
|
||||
*/
|
||||
@Test
|
||||
public void testGetCountOfItems() {
|
||||
public void testsize() {
|
||||
//arrange
|
||||
Queue<String> queue = new Queue<>();
|
||||
PowerQueue<String> queue = new PowerQueue<>();
|
||||
int expectedCountOfItems = 0;
|
||||
int returnedCountOfItems;
|
||||
//act
|
||||
returnedCountOfItems = queue.getCountOfItems();
|
||||
returnedCountOfItems = queue.size();
|
||||
//assert
|
||||
assertEquals(expectedCountOfItems, returnedCountOfItems);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of getCountOfItems method, of class Queue.
|
||||
* Test of size method, of class Queue.
|
||||
*/
|
||||
@Test
|
||||
public void testGetCountOfItems2() {
|
||||
public void testsize2() {
|
||||
//arrange
|
||||
Queue<String> queue = new Queue<>();
|
||||
PowerQueue<String> queue = new PowerQueue<>();
|
||||
int expectedCountOfItems = 0;
|
||||
int returnedCountOfItems;
|
||||
//act
|
||||
queue.enqueue("John");
|
||||
queue.dequeue();
|
||||
returnedCountOfItems = queue.getCountOfItems();
|
||||
queue.add("John");
|
||||
queue.poll();
|
||||
returnedCountOfItems = queue.size();
|
||||
//assert
|
||||
assertEquals(expectedCountOfItems, returnedCountOfItems);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of getCountOfItems method, of class Queue.
|
||||
* Test of size method, of class Queue.
|
||||
*/
|
||||
@Test
|
||||
public void testGetCountOfItems3() {
|
||||
public void testsize3() {
|
||||
//arrange
|
||||
Queue<String> queue = new Queue<>();
|
||||
PowerQueue<String> queue = new PowerQueue<>();
|
||||
int expectedCountOfItems = 0;
|
||||
int returnedCountOfItems;
|
||||
//act
|
||||
queue.enqueue("John");
|
||||
queue.dequeue();
|
||||
queue.enqueue("Jack").enqueue("Anne").enqueue("Charlie");
|
||||
queue.add("John");
|
||||
queue.poll();
|
||||
queue.addAndReturn("Jack").addAndReturn("Anne").addAndReturn("Charlie");
|
||||
|
||||
queue.dequeue();
|
||||
queue.dequeue();
|
||||
queue.dequeue();
|
||||
queue.enqueue("Jane");
|
||||
queue.dequeue();
|
||||
returnedCountOfItems = queue.getCountOfItems();
|
||||
queue.poll();
|
||||
queue.poll();
|
||||
queue.poll();
|
||||
queue.add("Jane");
|
||||
queue.poll();
|
||||
returnedCountOfItems = queue.size();
|
||||
//assert
|
||||
assertEquals(expectedCountOfItems, returnedCountOfItems);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of getCountOfItems method, of class Queue.
|
||||
* Test of size method, of class Queue.
|
||||
*/
|
||||
@Test
|
||||
public void testGetCountOfItems4() {
|
||||
public void testsize4() {
|
||||
//arrange
|
||||
Queue<String> queue = new Queue<>();
|
||||
PowerQueue<String> queue = new PowerQueue<>();
|
||||
int expectedCountOfItems = 1;
|
||||
int returnedCountOfItems;
|
||||
//act
|
||||
queue.enqueue("John");
|
||||
queue.dequeue();
|
||||
queue.enqueue("Jack").enqueue("Anne").enqueue("Charlie");
|
||||
queue.add("John");
|
||||
queue.poll();
|
||||
queue.addAndReturn("Jack").addAndReturn("Anne").addAndReturn("Charlie");
|
||||
|
||||
queue.dequeue();
|
||||
queue.dequeue();
|
||||
queue.enqueue("Jane");
|
||||
queue.dequeue();
|
||||
returnedCountOfItems = queue.getCountOfItems();
|
||||
queue.poll();
|
||||
queue.poll();
|
||||
queue.add("Jane");
|
||||
queue.poll();
|
||||
returnedCountOfItems = queue.size();
|
||||
//assert
|
||||
assertEquals(expectedCountOfItems, returnedCountOfItems);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of getCountOfItems method, of class Queue.
|
||||
* Test of size method, of class Queue.
|
||||
*/
|
||||
@Test
|
||||
public void testGetCountOfItems5() {
|
||||
public void testsize5() {
|
||||
//arrange
|
||||
Queue<String> queue = new Queue<>();
|
||||
PowerQueue<String> queue = new PowerQueue<>();
|
||||
int expectedCountOfItems = 3;
|
||||
int returnedCountOfItems;
|
||||
//act
|
||||
queue.enqueue("John");
|
||||
queue.dequeue();
|
||||
queue.enqueue("Jack").enqueue("Anne").enqueue("Charlie").enqueue("Susan");
|
||||
queue.add("John");
|
||||
queue.poll();
|
||||
queue.addAndReturn("Jack").addAndReturn("Anne").addAndReturn("Charlie").addAndReturn("Susan");
|
||||
|
||||
queue.dequeue();
|
||||
queue.enqueue("Jane");
|
||||
queue.dequeue();
|
||||
returnedCountOfItems = queue.getCountOfItems();
|
||||
queue.poll();
|
||||
queue.add("Jane");
|
||||
queue.poll();
|
||||
returnedCountOfItems = queue.size();
|
||||
//assert
|
||||
assertEquals(expectedCountOfItems, returnedCountOfItems);
|
||||
}
|
||||
@ -143,7 +148,7 @@ public class QueueTest {
|
||||
@Test
|
||||
public void testIsEmpty() {
|
||||
//arrange
|
||||
Queue<String> queue = new Queue<>();
|
||||
PowerQueue<String> queue = new PowerQueue<>();
|
||||
boolean expectedValue = true;
|
||||
boolean returnedValue;
|
||||
//act
|
||||
@ -158,19 +163,19 @@ public class QueueTest {
|
||||
@Test
|
||||
public void testIsEmpty2() {
|
||||
//arrange
|
||||
Queue<String> queue = new Queue<>();
|
||||
PowerQueue<String> queue = new PowerQueue<>();
|
||||
boolean expectedValue = true;
|
||||
boolean returnedValue;
|
||||
//act
|
||||
queue.enqueue("John").enqueue("Jack").enqueue("Anne").enqueue("Charlie");
|
||||
queue.addAndReturn("John").addAndReturn("Jack").addAndReturn("Anne").addAndReturn("Charlie");
|
||||
|
||||
queue.dequeue();
|
||||
queue.dequeue();
|
||||
queue.dequeue();
|
||||
queue.dequeue();
|
||||
queue.poll();
|
||||
queue.poll();
|
||||
queue.poll();
|
||||
queue.poll();
|
||||
|
||||
queue.enqueue("Jane");
|
||||
queue.dequeue();
|
||||
queue.add("Jane");
|
||||
queue.poll();
|
||||
returnedValue = queue.isEmpty();
|
||||
//assert
|
||||
assertEquals(expectedValue, returnedValue);
|
||||
@ -182,18 +187,18 @@ public class QueueTest {
|
||||
@Test
|
||||
public void testIsEmpty3() {
|
||||
//arrange
|
||||
Queue<String> queue = new Queue<>();
|
||||
PowerQueue<String> queue = new PowerQueue<>();
|
||||
boolean expectedValue = false;
|
||||
boolean returnedValue;
|
||||
//act
|
||||
queue.enqueue("John").enqueue("Jack").enqueue("Anne").enqueue("Charlie");
|
||||
queue.addAndReturn("John").addAndReturn("Jack").addAndReturn("Anne").addAndReturn("Charlie");
|
||||
|
||||
queue.dequeue();
|
||||
queue.dequeue();
|
||||
queue.dequeue();
|
||||
queue.poll();
|
||||
queue.poll();
|
||||
queue.poll();
|
||||
|
||||
queue.enqueue("Jane");
|
||||
queue.dequeue();
|
||||
queue.add("Jane");
|
||||
queue.poll();
|
||||
returnedValue = queue.isEmpty();
|
||||
//assert
|
||||
assertEquals(expectedValue, returnedValue);
|
||||
@ -205,17 +210,17 @@ public class QueueTest {
|
||||
@Test
|
||||
public void testIsEmpty4() {
|
||||
//arrange
|
||||
Queue<String> queue = new Queue<>();
|
||||
PowerQueue<String> queue = new PowerQueue<>();
|
||||
boolean expectedValue = false;
|
||||
boolean returnedValue;
|
||||
//act
|
||||
queue.enqueue("John").enqueue("Jack").enqueue("Anne").enqueue("Charlie");
|
||||
queue.addAndReturn("John").addAndReturn("Jack").addAndReturn("Anne").addAndReturn("Charlie");
|
||||
|
||||
queue.dequeue();
|
||||
queue.dequeue();
|
||||
queue.poll();
|
||||
queue.poll();
|
||||
|
||||
queue.enqueue("Jane");
|
||||
queue.dequeue();
|
||||
queue.add("Jane");
|
||||
queue.poll();
|
||||
returnedValue = queue.isEmpty();
|
||||
//assert
|
||||
assertEquals(expectedValue, returnedValue);
|
||||
@ -227,12 +232,12 @@ public class QueueTest {
|
||||
@Test
|
||||
public void testEnqueue() {
|
||||
//arrange
|
||||
Queue<String> queue = new Queue<>();
|
||||
PowerQueue<String> queue = new PowerQueue<>();
|
||||
String expectedString = "John";
|
||||
String returnedString;
|
||||
//act
|
||||
queue.enqueue("John");
|
||||
returnedString = queue.dequeue();
|
||||
queue.add("John");
|
||||
returnedString = queue.poll();
|
||||
//assert
|
||||
assertEquals(expectedString, returnedString);
|
||||
}
|
||||
@ -243,12 +248,12 @@ public class QueueTest {
|
||||
@Test
|
||||
public void testDequeue() {
|
||||
//arrange
|
||||
Queue<String> queue = new Queue<>();
|
||||
PowerQueue<String> queue = new PowerQueue<>();
|
||||
String expectedString = "Alice";
|
||||
String returnedString;
|
||||
//act
|
||||
queue.enqueue("Alice").enqueue("Jane");
|
||||
returnedString = queue.dequeue();
|
||||
queue.addAndReturn("Alice").addAndReturn("Jane");
|
||||
returnedString = queue.poll();
|
||||
//assert
|
||||
assertEquals(expectedString, returnedString);
|
||||
}
|
||||
@ -257,33 +262,50 @@ public class QueueTest {
|
||||
* Test of dequeue method, of class Queue.
|
||||
*/
|
||||
@Test
|
||||
public void testDequeue_ThereShouldBeThrownPowerRuntimeException() {
|
||||
public void testDequeue_ThereShouldBeThrownOkayRuntimeException() {
|
||||
//arrange
|
||||
boolean isExceptionThrown = false;
|
||||
Queue<String> queue = new Queue<>();
|
||||
PowerQueue<String> queue = new PowerQueue<>();
|
||||
//act
|
||||
try {
|
||||
queue.dequeue();
|
||||
} catch (PowerRuntimeException e) {
|
||||
queue.poll();
|
||||
} catch (NoSuchElementException e) {
|
||||
isExceptionThrown = true;
|
||||
}
|
||||
//assert
|
||||
if (!isExceptionThrown) {
|
||||
fail("There should be thrown PowerRuntimeException");
|
||||
fail("There should be thrown OkayRuntimeException");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of peek method, of class Stack.
|
||||
*/
|
||||
@Test
|
||||
public void testClear() {
|
||||
//arrange
|
||||
PowerQueue<String> queue = new PowerQueue<>();
|
||||
boolean returnedValue;
|
||||
//act
|
||||
queue.addAndReturn("John").addAndReturn("Jack").addAndReturn("Anne").addAndReturn("Charlie").addAndReturn("Alice").addAndReturn("Dennis");
|
||||
|
||||
queue.clear();
|
||||
returnedValue = queue.isEmpty();
|
||||
//assert
|
||||
assertTrue(returnedValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of toString method, of class Queue.
|
||||
*/
|
||||
@Test
|
||||
public void testToString() {
|
||||
//arrange
|
||||
Queue<String> queue = new Queue<>();
|
||||
String expectedString = "Alice, Jane, ";
|
||||
PowerQueue<String> queue = new PowerQueue<>();
|
||||
String expectedString = "Alice, Jane";
|
||||
String returnedString;
|
||||
//act
|
||||
queue.enqueue("Alice").enqueue("Jane");
|
||||
queue.addAndReturn("Alice").addAndReturn("Jane");
|
||||
returnedString = queue.toString();
|
||||
//assert
|
||||
assertEquals(expectedString, returnedString);
|
@ -0,0 +1,164 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.collections;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.*;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
public class SingleLinkedListNodeTest {
|
||||
|
||||
/**
|
||||
* Test of link method, of class Node.
|
||||
*/
|
||||
@Test
|
||||
public void link() {
|
||||
//arrange
|
||||
SingleLinkedListNode<String> node1 = new SingleLinkedListNode<>("John");
|
||||
SingleLinkedListNode<String> node2 = new SingleLinkedListNode<>("Black");
|
||||
String expectedString = "Black";
|
||||
String returnedString;
|
||||
//act
|
||||
SingleLinkedListNode.link(node1, node2);
|
||||
SingleLinkedListNode<String> returned = node1.getNext();
|
||||
returnedString = returned.getValue();
|
||||
//assert
|
||||
assertEquals(expectedString, returnedString);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of getValue method, of class Node.
|
||||
*/
|
||||
@Test
|
||||
public void getValue() {
|
||||
//arrange
|
||||
SingleLinkedListNode<String> node = new SingleLinkedListNode<>("John");
|
||||
String expectedString = "John";
|
||||
String returnedString;
|
||||
//act
|
||||
returnedString = node.getValue();
|
||||
//assert
|
||||
assertEquals(expectedString, returnedString);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of setValue method, of class Node.
|
||||
*/
|
||||
@Test
|
||||
public void setValue() {
|
||||
//arrange
|
||||
SingleLinkedListNode<String> node = new SingleLinkedListNode<>("John");
|
||||
String expectedString = "Jack";
|
||||
String returnedString;
|
||||
//act
|
||||
node.setValue("Jack");
|
||||
returnedString = node.getValue();
|
||||
//assert
|
||||
assertEquals(expectedString, returnedString);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of getNext method, of class Node.
|
||||
*/
|
||||
@Test
|
||||
public void getNext() {
|
||||
//arrange
|
||||
SingleLinkedListNode<String> node1 = new SingleLinkedListNode<>("John");
|
||||
SingleLinkedListNode<String> node2 = new SingleLinkedListNode<>("Black");
|
||||
String expectedString = "Black";
|
||||
String returnedString;
|
||||
//act
|
||||
node1.setNext(node2);
|
||||
SingleLinkedListNode<String> returnedmapNode = node1.getNext();
|
||||
returnedString = returnedmapNode.getValue();
|
||||
//assert
|
||||
assertEquals(expectedString, returnedString);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of setNext method, of class Node.
|
||||
*/
|
||||
@Test
|
||||
public void setNext() {
|
||||
//arrange
|
||||
SingleLinkedListNode<String> node1 = new SingleLinkedListNode<>("John");
|
||||
SingleLinkedListNode<String> node2 = new SingleLinkedListNode<>("White");
|
||||
String expectedString = "White";
|
||||
String returnedString;
|
||||
//act
|
||||
node1.setNext(node2);
|
||||
SingleLinkedListNode<String> returned = node1.getNext();
|
||||
returnedString = returned.getValue();
|
||||
//assert
|
||||
assertEquals(expectedString, returnedString);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of hasNext method, of class Node.
|
||||
*/
|
||||
@Test
|
||||
public void hasNext() {
|
||||
//arrange
|
||||
SingleLinkedListNode<String> node1 = new SingleLinkedListNode<>("John");
|
||||
//act
|
||||
|
||||
//assert
|
||||
assertFalse(node1.hasNext());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of hasNext method, of class Node.
|
||||
*/
|
||||
@Test
|
||||
public void hasNext2() {
|
||||
//arrange
|
||||
SingleLinkedListNode<String> node1 = new SingleLinkedListNode<>("John");
|
||||
SingleLinkedListNode<String> node2 = new SingleLinkedListNode<>("White");
|
||||
//act
|
||||
node1.setNext(node2);
|
||||
//assert
|
||||
assertTrue(node1.hasNext());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of unlinkNext method, of class Node.
|
||||
*/
|
||||
@Test
|
||||
public void unlinkNext() {
|
||||
//arrange
|
||||
SingleLinkedListNode<String> node1 = new SingleLinkedListNode<>("John");
|
||||
SingleLinkedListNode<String> node2 = new SingleLinkedListNode<>("White");
|
||||
//act
|
||||
assertFalse(node1.hasNext());
|
||||
node1.setNext(node2);
|
||||
assertTrue(node1.hasNext());
|
||||
node1.unlinkNext();
|
||||
//assert
|
||||
assertFalse(node1.hasNext());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,782 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.collections;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.nanoboot.powerframework.core.exceptions.NotYetImplementedException;
|
||||
import org.junit.*;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
public class SingleLinkedListTest {
|
||||
@Test
|
||||
public void addAll_EVarArg() {
|
||||
//arrange
|
||||
SingleLinkedList<String> linkedList = new SingleLinkedList<>();
|
||||
|
||||
//act
|
||||
linkedList
|
||||
.addAll("a", "b", "c", "d", "e");
|
||||
//assert
|
||||
assertEquals(5, linkedList.size());
|
||||
assertEquals("a, b, c, d, e", linkedList.toString());
|
||||
assertEquals("a", linkedList.get(0));
|
||||
assertEquals("b", linkedList.get(1));
|
||||
assertEquals("c", linkedList.get(2));
|
||||
assertEquals("d", linkedList.get(3));
|
||||
assertEquals("e", linkedList.get(4));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void contains() {
|
||||
//arrange
|
||||
SingleLinkedList<String> linkedList = new SingleLinkedList<>();
|
||||
|
||||
//act
|
||||
linkedList
|
||||
.addAll("a", "b", "c", "d", "e");
|
||||
//assert
|
||||
assertTrue(linkedList.contains("a"));
|
||||
assertTrue(linkedList.contains("b"));
|
||||
assertTrue(linkedList.contains("c"));
|
||||
assertTrue(linkedList.contains("d"));
|
||||
assertTrue(linkedList.contains("e"));
|
||||
assertFalse(linkedList.contains("f"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void iterator() {
|
||||
//arrange
|
||||
SingleLinkedList<String> linkedList = new SingleLinkedList<>();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String expected = "12345";
|
||||
String returned;
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
linkedList.add(String.valueOf(i));
|
||||
}
|
||||
//act
|
||||
for (String e : linkedList) {
|
||||
sb.append(e);
|
||||
}
|
||||
returned = sb.toString();
|
||||
//assert
|
||||
assertEquals(expected, returned);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toArray_ObjectArray() {
|
||||
//arrange
|
||||
SingleLinkedList<String> linkedList = new SingleLinkedList<>();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String expected = "12345";
|
||||
String returned;
|
||||
Object[] array;
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
linkedList.add(String.valueOf(i));
|
||||
}
|
||||
//act
|
||||
array = linkedList.toArray();
|
||||
for (Object o : array) {
|
||||
sb.append(o);
|
||||
}
|
||||
returned = sb.toString();
|
||||
//assert
|
||||
assertEquals(5, array.length);
|
||||
assertEquals(expected, returned);
|
||||
}
|
||||
|
||||
@Test(expected = NotYetImplementedException.class)
|
||||
public void toArray_genericArray() {
|
||||
//arrange
|
||||
SingleLinkedList<String> linkedList = new SingleLinkedList<>();
|
||||
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
linkedList.add(String.valueOf(i));
|
||||
}
|
||||
//act
|
||||
linkedList.toArray(new Object[]{});
|
||||
//assert
|
||||
}
|
||||
|
||||
@Test
|
||||
public void add() {
|
||||
//arrange
|
||||
SingleLinkedList<String> linkedList = new SingleLinkedList<>();
|
||||
//act
|
||||
//assert
|
||||
linkedList.add("a");
|
||||
assertEquals(1, linkedList.size());
|
||||
assertEquals("a", linkedList.toString());
|
||||
linkedList.add("b");
|
||||
assertEquals(2, linkedList.size());
|
||||
assertEquals("a, b", linkedList.toString());
|
||||
linkedList.add("c");
|
||||
assertEquals(3, linkedList.size());
|
||||
assertEquals("a, b, c", linkedList.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void remove_Object() {
|
||||
//arrange
|
||||
SingleLinkedList<String> linkedList = new SingleLinkedList<>();
|
||||
//act
|
||||
linkedList.add("a");
|
||||
linkedList.add("b");
|
||||
linkedList.add("c");
|
||||
//assert
|
||||
//abc
|
||||
assertEquals(3, linkedList.size());
|
||||
linkedList.remove("b");
|
||||
//ac
|
||||
assertEquals(2, linkedList.size());
|
||||
assertFalse(linkedList.contains("b"));
|
||||
linkedList.add("d");
|
||||
//acd
|
||||
assertEquals(3, linkedList.size());
|
||||
assertTrue(linkedList.contains("d"));
|
||||
linkedList.remove("a");
|
||||
//cd
|
||||
assertEquals(2, linkedList.size());
|
||||
assertFalse(linkedList.contains("a"));
|
||||
linkedList.remove("d");
|
||||
//c
|
||||
assertEquals(1, linkedList.size());
|
||||
assertFalse(linkedList.contains("d"));
|
||||
assertTrue(linkedList.contains("c"));
|
||||
linkedList.remove("c");
|
||||
assertEquals(0, linkedList.size());
|
||||
assertFalse(linkedList.contains("c"));
|
||||
//
|
||||
assertEquals("", linkedList.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void containsAll() {
|
||||
//arrange
|
||||
SingleLinkedList<String> linkedList = new SingleLinkedList<>();
|
||||
//act
|
||||
linkedList.add("a");
|
||||
linkedList.add("b");
|
||||
linkedList.add("c");
|
||||
//assert
|
||||
assertTrue(linkedList.containsAll(new SingleLinkedList<>("a", "b", "c")));
|
||||
assertTrue(linkedList.containsAll(new SingleLinkedList<>("b", "c")));
|
||||
assertTrue(linkedList.containsAll(new SingleLinkedList<>("a", "c")));
|
||||
assertTrue(linkedList.containsAll(new SingleLinkedList<>("a", "b")));
|
||||
assertTrue(linkedList.containsAll(new SingleLinkedList<>("a")));
|
||||
assertTrue(linkedList.containsAll(new SingleLinkedList<>("b")));
|
||||
assertTrue(linkedList.containsAll(new SingleLinkedList<>("c")));
|
||||
assertFalse(linkedList.containsAll(new SingleLinkedList<>("a", "b", "c", "d")));
|
||||
assertFalse(linkedList.containsAll(new SingleLinkedList<>("b", "c", "d")));
|
||||
assertFalse(linkedList.containsAll(new SingleLinkedList<>("a", "c", "d")));
|
||||
assertFalse(linkedList.containsAll(new SingleLinkedList<>("a", "b", "d")));
|
||||
assertFalse(linkedList.containsAll(new SingleLinkedList<>("a", "d")));
|
||||
assertFalse(linkedList.containsAll(new SingleLinkedList<>("b", "d")));
|
||||
assertFalse(linkedList.containsAll(new SingleLinkedList<>("c", "d")));
|
||||
assertTrue(linkedList.containsAll(new SingleLinkedList<>()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddAll_collection() {
|
||||
//arrange
|
||||
SingleLinkedList<String> linkedList = new SingleLinkedList<>();
|
||||
//act
|
||||
linkedList.addAll(new SingleLinkedList<>("a", "b", "c"));
|
||||
//assert
|
||||
assertEquals(3, linkedList.size());
|
||||
assertEquals("a, b, c", linkedList.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeAll() {
|
||||
//arrange
|
||||
SingleLinkedList<String> linkedList = new SingleLinkedList<>();
|
||||
//act
|
||||
linkedList.addAll(new SingleLinkedList<>("a", "b", "c"));
|
||||
linkedList.removeAll(new SingleLinkedList<>("a", "c"));
|
||||
//assert
|
||||
assertEquals(1, linkedList.size());
|
||||
assertEquals("b", linkedList.toString());
|
||||
linkedList.removeAll(new SingleLinkedList<>("b"));
|
||||
assertEquals(0, linkedList.size());
|
||||
assertEquals("", linkedList.toString());
|
||||
}
|
||||
|
||||
@Test(expected = NotYetImplementedException.class)
|
||||
public void retainAll() {
|
||||
//arrange
|
||||
SingleLinkedList<String> linkedList = new SingleLinkedList<>();
|
||||
//act
|
||||
linkedList.addAll(new SingleLinkedList<>("a", "b", "c"));
|
||||
linkedList.retainAll(new SingleLinkedList<>("a", "c"));
|
||||
//assert
|
||||
}
|
||||
|
||||
@Test
|
||||
public void clear() {
|
||||
//arrange
|
||||
SingleLinkedList<String> linkedList = new SingleLinkedList<>();
|
||||
//act
|
||||
linkedList.addAll(new SingleLinkedList<>("a", "b", "c"));
|
||||
//assert
|
||||
assertEquals(3, linkedList.size());
|
||||
linkedList.clear();
|
||||
assertEquals(0, linkedList.size());
|
||||
assertEquals("", linkedList.toString());
|
||||
linkedList.add("d");
|
||||
assertEquals(1, linkedList.size());
|
||||
assertEquals("d", linkedList.toString());
|
||||
linkedList.add("e");
|
||||
assertEquals(2, linkedList.size());
|
||||
assertEquals("d, e", linkedList.toString());
|
||||
linkedList.clear();
|
||||
assertEquals(0, linkedList.size());
|
||||
assertEquals("", linkedList.toString());
|
||||
}
|
||||
|
||||
//
|
||||
@Test
|
||||
public void addBeforeFirst() {
|
||||
//arrange
|
||||
SingleLinkedList<String> linkedList = new SingleLinkedList<>();
|
||||
|
||||
//act
|
||||
|
||||
//assert
|
||||
linkedList.addBeforeFirst("a");
|
||||
assertEquals(1, linkedList.size());
|
||||
assertEquals("a", linkedList.toString());
|
||||
linkedList.addBeforeFirst("b");
|
||||
assertEquals(2, linkedList.size());
|
||||
assertEquals("b, a", linkedList.toString());
|
||||
linkedList.addBeforeFirst("c");
|
||||
assertEquals(3, linkedList.size());
|
||||
assertEquals("c, b, a", linkedList.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addAfterLast() {
|
||||
//arrange
|
||||
SingleLinkedList<String> linkedList = new SingleLinkedList<>();
|
||||
|
||||
//act
|
||||
|
||||
//assert
|
||||
linkedList.addAfterLast("a");
|
||||
assertEquals(1, linkedList.size());
|
||||
assertEquals("a", linkedList.toString());
|
||||
linkedList.addAfterLast("b");
|
||||
assertEquals(2, linkedList.size());
|
||||
assertEquals("a, b", linkedList.toString());
|
||||
linkedList.addAfterLast("c");
|
||||
assertEquals(3, linkedList.size());
|
||||
assertEquals("a, b, c", linkedList.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeFirst() {
|
||||
//arrange
|
||||
SingleLinkedList<String> linkedList = new SingleLinkedList<>();
|
||||
String expectedString = "AnneJohnGeorgeKate";
|
||||
String returnedString;
|
||||
linkedList.addAfterLast("Jack")
|
||||
;
|
||||
linkedList.addAfterLast("Anne")
|
||||
;
|
||||
linkedList.addAfterLast("John")
|
||||
;
|
||||
linkedList.addAfterLast("George")
|
||||
;
|
||||
linkedList.addAfterLast("Kate");
|
||||
//act
|
||||
linkedList.removeFirst();
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
for (String element : linkedList) {
|
||||
stringBuilder.append(element);
|
||||
}
|
||||
returnedString = stringBuilder.toString();
|
||||
//assert
|
||||
assertEquals(expectedString, returnedString);
|
||||
linkedList.removeFirst();
|
||||
assertEquals(3, linkedList.size());
|
||||
assertEquals("John, George, Kate", linkedList.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeLast() {
|
||||
//arrange
|
||||
SingleLinkedList<String> linkedList = new SingleLinkedList<>();
|
||||
String expectedString = "JackAnneJohnGeorge";
|
||||
String returnedString;
|
||||
linkedList.addAfterLast("Jack")
|
||||
;
|
||||
linkedList.addAfterLast("Anne")
|
||||
;
|
||||
linkedList.addAfterLast("John")
|
||||
;
|
||||
linkedList.addAfterLast("George")
|
||||
;
|
||||
linkedList.addAfterLast("Kate");
|
||||
//act
|
||||
linkedList.removeLast();
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
for (String element : linkedList) {
|
||||
stringBuilder.append(element);
|
||||
}
|
||||
returnedString = stringBuilder.toString();
|
||||
//assert
|
||||
assertEquals(expectedString, returnedString);
|
||||
linkedList.removeLast();
|
||||
|
||||
assertEquals(3, linkedList.size());
|
||||
assertEquals("Jack, Anne, John", linkedList.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getFirst() {
|
||||
//arrange
|
||||
SingleLinkedList<String> linkedList = new SingleLinkedList<>();
|
||||
String expectedString = "Jack";
|
||||
String returnedString;
|
||||
linkedList.addAfterLast("Jack");
|
||||
linkedList.addAfterLast("Anne");
|
||||
linkedList.addAfterLast("John");
|
||||
linkedList.addAfterLast("George");
|
||||
linkedList.addAfterLast("Kate");
|
||||
//act
|
||||
returnedString = linkedList.getFirst();
|
||||
//assert
|
||||
assertEquals(expectedString, returnedString);
|
||||
linkedList.removeFirst();
|
||||
assertEquals("Anne", linkedList.getFirst());
|
||||
assertEquals("Anne, John, George, Kate", linkedList.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getLast() {
|
||||
//arrange
|
||||
SingleLinkedList<String> linkedList = new SingleLinkedList<>();
|
||||
String expectedString = "George";
|
||||
String returnedString;
|
||||
linkedList.addAfterLast("Jack");
|
||||
linkedList.addAfterLast("Anne");
|
||||
linkedList.addAfterLast("John");
|
||||
linkedList.addAfterLast("George");
|
||||
linkedList.addAfterLast("Kate");
|
||||
//act
|
||||
linkedList.addAfterLast("Mike");
|
||||
linkedList.addBeforeFirst("William");
|
||||
linkedList.removeFirst();
|
||||
linkedList.removeLast();
|
||||
linkedList.removeFirst();
|
||||
linkedList.addAfterLast("Earl");
|
||||
linkedList.removeLast();
|
||||
linkedList.removeFirst();
|
||||
linkedList.removeLast();
|
||||
|
||||
returnedString = linkedList.getLast();
|
||||
//assert
|
||||
assertEquals(expectedString, returnedString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void remove_int() {
|
||||
//arrange
|
||||
SingleLinkedList<String> linkedList = new SingleLinkedList<>();
|
||||
//act
|
||||
//assert
|
||||
linkedList.add("a");
|
||||
linkedList.add("b");
|
||||
linkedList.add("c");
|
||||
//abc
|
||||
assertEquals(3, linkedList.size());
|
||||
linkedList.remove(1);
|
||||
assertEquals("a, c", linkedList.toString());
|
||||
//ac
|
||||
assertEquals(2, linkedList.size());
|
||||
assertFalse(linkedList.contains("b"));
|
||||
linkedList.add("d");
|
||||
assertEquals("a, c, d", linkedList.toString());
|
||||
//acd
|
||||
assertEquals(3, linkedList.size());
|
||||
assertTrue(linkedList.contains("d"));
|
||||
linkedList.remove(0);
|
||||
//cd
|
||||
assertEquals(2, linkedList.size());
|
||||
assertFalse(linkedList.contains("a"));
|
||||
linkedList.remove(1);
|
||||
//c
|
||||
assertEquals(1, linkedList.size());
|
||||
assertFalse(linkedList.contains("d"));
|
||||
assertTrue(linkedList.contains("c"));
|
||||
linkedList.remove(0);
|
||||
assertEquals(0, linkedList.size());
|
||||
assertFalse(linkedList.contains("c"));
|
||||
//
|
||||
assertEquals("", linkedList.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void get() {
|
||||
//arrange
|
||||
SingleLinkedList<String> linkedList = new SingleLinkedList<>();
|
||||
linkedList.addAfterLast("Jack");
|
||||
linkedList.addAfterLast("Anne");
|
||||
linkedList.addAfterLast("John");
|
||||
linkedList.addAfterLast("George");
|
||||
linkedList.addAfterLast("Kate");
|
||||
//act
|
||||
|
||||
//assert
|
||||
assertEquals("Jack", linkedList.get(0));
|
||||
assertEquals("Anne", linkedList.get(1));
|
||||
assertEquals("John", linkedList.get(2));
|
||||
assertEquals("George", linkedList.get(3));
|
||||
assertEquals("Kate", linkedList.get(4));
|
||||
}
|
||||
|
||||
@Test(expected = NoSuchElementException.class)
|
||||
public void get2() {
|
||||
//arrange
|
||||
SingleLinkedList<String> linkedList = new SingleLinkedList<>();
|
||||
//act
|
||||
|
||||
//assert
|
||||
assertEquals("Jack", linkedList.get(0));
|
||||
|
||||
}
|
||||
|
||||
@Test(expected = CollectionException.class)
|
||||
public void get3() {
|
||||
//arrange
|
||||
SingleLinkedList<String> linkedList = new SingleLinkedList<>();
|
||||
linkedList.addAfterLast("Jack");
|
||||
linkedList.addAfterLast("Anne");
|
||||
linkedList.addAfterLast("John");
|
||||
linkedList.addAfterLast("Kate");
|
||||
//act
|
||||
linkedList.get(4);
|
||||
//assert
|
||||
}
|
||||
|
||||
@Test
|
||||
public void set() {
|
||||
//arrange
|
||||
SingleLinkedList<String> linkedList = new SingleLinkedList<>();
|
||||
linkedList.addAfterLast("a");
|
||||
linkedList.addAfterLast("b");
|
||||
linkedList.addAfterLast("c");
|
||||
linkedList.addAfterLast("d");
|
||||
linkedList.addAfterLast("e");
|
||||
//act
|
||||
|
||||
//assert
|
||||
assertEquals("abcde", linkedList.toString(""));
|
||||
linkedList.set(2, "f");
|
||||
assertEquals("abfde", linkedList.toString(""));
|
||||
linkedList.set(3, "g");
|
||||
assertEquals("abfge", linkedList.toString(""));
|
||||
linkedList.set(0, "h");
|
||||
assertEquals("hbfge", linkedList.toString(""));
|
||||
linkedList.set(3, "i");
|
||||
assertEquals("hbfie", linkedList.toString(""));
|
||||
linkedList.set(1, "j");
|
||||
assertEquals("hjfie", linkedList.toString(""));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of toString method, of class LinkedList.
|
||||
*/
|
||||
@Test
|
||||
public void testToString() {
|
||||
//arrange
|
||||
SingleLinkedList<String> linkedList = new SingleLinkedList<>();
|
||||
String expectedString = "Jack, Anne, John, George, Kate";
|
||||
String returnedString;
|
||||
linkedList.addAfterLast("Jack");
|
||||
linkedList.addAfterLast("Anne");
|
||||
linkedList.addAfterLast("John");
|
||||
linkedList.addAfterLast("George");
|
||||
linkedList.addAfterLast("Kate");
|
||||
//act
|
||||
returnedString = linkedList.toString();
|
||||
//assert
|
||||
assertEquals(expectedString, returnedString);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of toString method, of class LinkedList.
|
||||
*/
|
||||
@Test
|
||||
public void testToString_2() {
|
||||
//arrange
|
||||
SingleLinkedList<String> linkedList = new SingleLinkedList<>();
|
||||
String expectedString = "";
|
||||
String returnedString;
|
||||
//act
|
||||
returnedString = linkedList.toString();
|
||||
//assert
|
||||
assertEquals(expectedString, returnedString);
|
||||
}
|
||||
|
||||
//////
|
||||
|
||||
/**
|
||||
* Test of getsize method, of class LinkedList.
|
||||
*/
|
||||
@Test
|
||||
public void size_justCreatedInstanceShouldHaveCountZero() {
|
||||
//arrange
|
||||
SingleLinkedList<String> linkedList = new SingleLinkedList<>();
|
||||
int expectedCountOfItems = 0;
|
||||
int returnedCountOfItems;
|
||||
//act
|
||||
returnedCountOfItems = linkedList.size();
|
||||
//assert
|
||||
assertEquals(expectedCountOfItems, returnedCountOfItems);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of getsize method, of class LinkedList.
|
||||
*/
|
||||
@Test
|
||||
public void size_shouldHaveCountFive() {
|
||||
//arrange
|
||||
SingleLinkedList<String> linkedList = new SingleLinkedList<>();
|
||||
int expectedCountOfItems = 5;
|
||||
int returnedCountOfItems;
|
||||
|
||||
linkedList.addAfterLast("Jack");
|
||||
linkedList.addAfterLast("Anne");
|
||||
linkedList.addAfterLast("John");
|
||||
linkedList.addAfterLast("George");
|
||||
linkedList.addAfterLast("Kate");
|
||||
//act
|
||||
returnedCountOfItems = linkedList.size();
|
||||
//assert
|
||||
assertEquals(expectedCountOfItems, returnedCountOfItems);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of getsize method, of class LinkedList.
|
||||
*/
|
||||
@Test
|
||||
public void size_shouldHaveCountFourAfterFirstRemoved() {
|
||||
//arrange
|
||||
SingleLinkedList<String> linkedList = new SingleLinkedList<>();
|
||||
int expectedCountOfItems = 4;
|
||||
int returnedCountOfItems;
|
||||
|
||||
linkedList.addAfterLast("Jack");
|
||||
linkedList.addAfterLast("Anne");
|
||||
linkedList.addAfterLast("John");
|
||||
linkedList.addAfterLast("George");
|
||||
linkedList.addAfterLast("Kate");
|
||||
//act
|
||||
linkedList.removeFirst();
|
||||
returnedCountOfItems = linkedList.size();
|
||||
//assert
|
||||
assertEquals(expectedCountOfItems, returnedCountOfItems);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of getsize method, of class LinkedList.
|
||||
*/
|
||||
@Test
|
||||
public void size_shouldHaveCountThreeAfterTwoFirstRemoved() {
|
||||
//arrange
|
||||
SingleLinkedList<String> linkedList = new SingleLinkedList<>();
|
||||
int expectedCountOfItems = 3;
|
||||
int returnedCountOfItems;
|
||||
|
||||
linkedList.addAfterLast("Jack");
|
||||
linkedList.addAfterLast("Anne");
|
||||
linkedList.addAfterLast("John");
|
||||
linkedList.addAfterLast("George");
|
||||
linkedList.addAfterLast("Kate");
|
||||
//act
|
||||
linkedList.removeFirst();
|
||||
linkedList.removeFirst();
|
||||
returnedCountOfItems = linkedList.size();
|
||||
//assert
|
||||
assertEquals(expectedCountOfItems, returnedCountOfItems);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of getsize method, of class LinkedList.
|
||||
*/
|
||||
@Test
|
||||
public void size_shouldHaveCountFourAfterLastRemoved() {
|
||||
//arrange
|
||||
SingleLinkedList<String> linkedList = new SingleLinkedList<>();
|
||||
int expectedCountOfItems = 4;
|
||||
linkedList.addAfterLast("Jack");
|
||||
linkedList.addAfterLast("Anne");
|
||||
linkedList.addAfterLast("John");
|
||||
linkedList.addAfterLast("George");
|
||||
linkedList.addAfterLast("Kate");
|
||||
//act
|
||||
linkedList.removeLast();
|
||||
//assert
|
||||
assertEquals(expectedCountOfItems, linkedList.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of getsize method, of class LinkedList.
|
||||
*/
|
||||
@Test
|
||||
public void size_shouldHaveCountThreeAfterLastTwoRemoved() {
|
||||
//arrange
|
||||
SingleLinkedList<String> linkedList = new SingleLinkedList<>();
|
||||
int expectedCountOfItems = 3;
|
||||
linkedList.addAfterLast("Jack");
|
||||
linkedList.addAfterLast("Anne");
|
||||
linkedList.addAfterLast("John");
|
||||
linkedList.addAfterLast("George");
|
||||
linkedList.addAfterLast("Kate");
|
||||
//act
|
||||
linkedList.removeLast();
|
||||
linkedList.removeLast();
|
||||
//assert
|
||||
assertEquals(expectedCountOfItems, linkedList.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of getsize method, of class LinkedList.
|
||||
*/
|
||||
@Test
|
||||
public void size_shouldHaveCountSixAfterAddedBeforeFirst() {
|
||||
//arrange
|
||||
SingleLinkedList<String> linkedList = new SingleLinkedList<>();
|
||||
int expectedCountOfItems = 6;
|
||||
linkedList.addAfterLast("Jack");
|
||||
linkedList.addAfterLast("Anne");
|
||||
linkedList.addAfterLast("John");
|
||||
linkedList.addAfterLast("George");
|
||||
linkedList.addAfterLast("Kate");
|
||||
;
|
||||
linkedList.addBeforeFirst("Mike");
|
||||
//assert
|
||||
assertEquals(expectedCountOfItems, linkedList.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of getsize method, of class LinkedList.
|
||||
*/
|
||||
@Test
|
||||
public void size_shouldHaveCountSixAfterAddedAfterLast() {
|
||||
//arrange
|
||||
SingleLinkedList<String> linkedList = new SingleLinkedList<>();
|
||||
int expectedCountOfItems = 6;
|
||||
linkedList.addAfterLast("Jack");
|
||||
linkedList.addAfterLast("Anne");
|
||||
linkedList.addAfterLast("John");
|
||||
linkedList.addAfterLast("George");
|
||||
linkedList.addAfterLast("Kate");
|
||||
//act
|
||||
linkedList.addAfterLast("Mike");
|
||||
//assert
|
||||
assertEquals(expectedCountOfItems, linkedList.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of isEmpty method, of class LinkedList.
|
||||
*/
|
||||
@Test
|
||||
public void isEmpty_justCreatedInstanceShouldBeEmpty() {
|
||||
//arrange
|
||||
SingleLinkedList<String> linkedList = new SingleLinkedList<>();
|
||||
boolean returnedValue;
|
||||
//act
|
||||
returnedValue = linkedList.isEmpty();
|
||||
//assert
|
||||
assertTrue(returnedValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of isEmpty method, of class LinkedList.
|
||||
*/
|
||||
@Test
|
||||
public void isEmpty_linkedListShouldHaveFiveItemsAndShouldNotBeEmpty() {
|
||||
//arrange
|
||||
SingleLinkedList<String> linkedList = new SingleLinkedList<>();
|
||||
linkedList.addAfterLast("Jack");
|
||||
linkedList.addAfterLast("Anne");
|
||||
linkedList.addAfterLast("John");
|
||||
linkedList.addAfterLast("George");
|
||||
linkedList.addAfterLast("Kate");
|
||||
//act
|
||||
//assert
|
||||
assertFalse(linkedList.isEmpty());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of addBeforeFirst method, of class LinkedList.
|
||||
*/
|
||||
@Test
|
||||
public void isEmpty_countWasFive_fiveItemsWasRemoved_shouldBeEmpty_1() {
|
||||
//arrange
|
||||
SingleLinkedList<String> linkedList = new SingleLinkedList<>();
|
||||
linkedList.addAfterLast("Jack");
|
||||
linkedList.addAfterLast("Anne");
|
||||
linkedList.addAfterLast("John");
|
||||
linkedList.addAfterLast("George");
|
||||
linkedList.addAfterLast("Kate");
|
||||
//act
|
||||
linkedList.removeFirst();
|
||||
linkedList.removeLast();
|
||||
linkedList.removeLast();
|
||||
linkedList.removeFirst();
|
||||
linkedList.removeLast();
|
||||
//assert
|
||||
assertTrue(linkedList.isEmpty());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of addBeforeFirst method, of class LinkedList.
|
||||
*/
|
||||
@Test
|
||||
public void isEmpty_countWasFive_fiveItemsWasRemoved_shouldBeEmpty_2() {
|
||||
//arrange
|
||||
SingleLinkedList<String> linkedList = new SingleLinkedList<>();
|
||||
linkedList.addAfterLast("Jack");
|
||||
linkedList.addAfterLast("Anne");
|
||||
linkedList.addAfterLast("John");
|
||||
linkedList.addAfterLast("George");
|
||||
linkedList.addAfterLast("Kate");
|
||||
//act
|
||||
linkedList.removeFirst();
|
||||
linkedList.removeLast();
|
||||
linkedList.removeLast();
|
||||
linkedList.removeFirst();
|
||||
linkedList.removeFirst();
|
||||
//assert
|
||||
assertTrue(linkedList.isEmpty());
|
||||
}
|
||||
|
||||
}
|
@ -5,7 +5,7 @@
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
@ -20,14 +20,18 @@
|
||||
|
||||
package org.nanoboot.powerframework.collections;
|
||||
|
||||
import org.nanoboot.powerframework.PowerRuntimeException;
|
||||
import org.junit.Test;
|
||||
import java.util.*;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.*;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Robert Vokac e-mail: robertvokac@nanoboot.org
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
|
||||
public class StackTest {
|
||||
|
||||
/**
|
||||
@ -37,102 +41,102 @@ public class StackTest {
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of getCountOfItems method, of class Stack.
|
||||
* Test of size method, of class Stack.
|
||||
*/
|
||||
@Test
|
||||
public void testGetCountOfItems() {
|
||||
public void testsize() {
|
||||
//arrange
|
||||
Stack<String> stack = new Stack<>();
|
||||
PowerStack<String> stack = new PowerStack<>();
|
||||
int expectedCountOfItems = 0;
|
||||
int returnedCountOfItems;
|
||||
//act
|
||||
returnedCountOfItems = stack.getCountOfItems();
|
||||
returnedCountOfItems = stack.size();
|
||||
//assert
|
||||
assertEquals(expectedCountOfItems, returnedCountOfItems);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of getCountOfItems method, of class Stack.
|
||||
* Test of size method, of class Stack.
|
||||
*/
|
||||
@Test
|
||||
public void testGetCountOfItems2() {
|
||||
public void testsize2() {
|
||||
//arrange
|
||||
Stack<String> stack = new Stack<>();
|
||||
PowerStack<String> stack = new PowerStack<>();
|
||||
int expectedCountOfItems = 0;
|
||||
int returnedCountOfItems;
|
||||
//act
|
||||
stack.push("John");
|
||||
stack.pushAndReturn("John");
|
||||
stack.pop();
|
||||
returnedCountOfItems = stack.getCountOfItems();
|
||||
returnedCountOfItems = stack.size();
|
||||
//assert
|
||||
assertEquals(expectedCountOfItems, returnedCountOfItems);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of getCountOfItems method, of class Stack.
|
||||
* Test of size method, of class Stack.
|
||||
*/
|
||||
@Test
|
||||
public void testGetCountOfItems3() {
|
||||
public void testsize3() {
|
||||
//arrange
|
||||
Stack<String> stack = new Stack<>();
|
||||
PowerStack<String> stack = new PowerStack<>();
|
||||
int expectedCountOfItems = 0;
|
||||
int returnedCountOfItems;
|
||||
//act
|
||||
stack.push("John");
|
||||
stack.pushAndReturn("John");
|
||||
stack.pop();
|
||||
stack.push("Jack").push("Anne").push("Charlie");
|
||||
stack.pushAndReturn("Jack").pushAndReturn("Anne").pushAndReturn("Charlie");
|
||||
|
||||
stack.pop();
|
||||
stack.pop();
|
||||
stack.pop();
|
||||
stack.push("Jane");
|
||||
stack.pushAndReturn("Jane");
|
||||
stack.pop();
|
||||
returnedCountOfItems = stack.getCountOfItems();
|
||||
returnedCountOfItems = stack.size();
|
||||
//assert
|
||||
assertEquals(expectedCountOfItems, returnedCountOfItems);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of getCountOfItems method, of class Stack.
|
||||
* Test of size method, of class Stack.
|
||||
*/
|
||||
@Test
|
||||
public void testGetCountOfItems4() {
|
||||
public void testsize4() {
|
||||
//arrange
|
||||
Stack<String> stack = new Stack<>();
|
||||
PowerStack<String> stack = new PowerStack<>();
|
||||
int expectedCountOfItems = 1;
|
||||
int returnedCountOfItems;
|
||||
//act
|
||||
stack.push("John");
|
||||
stack.pushAndReturn("John");
|
||||
stack.pop();
|
||||
stack.push("Jack").push("Anne").push("Charlie");
|
||||
stack.pushAndReturn("Jack").pushAndReturn("Anne").pushAndReturn("Charlie");
|
||||
|
||||
stack.pop();
|
||||
stack.pop();
|
||||
stack.push("Jane");
|
||||
stack.pushAndReturn("Jane");
|
||||
stack.pop();
|
||||
returnedCountOfItems = stack.getCountOfItems();
|
||||
returnedCountOfItems = stack.size();
|
||||
//assert
|
||||
assertEquals(expectedCountOfItems, returnedCountOfItems);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of getCountOfItems method, of class Stack.
|
||||
* Test of size method, of class Stack.
|
||||
*/
|
||||
@Test
|
||||
public void testGetCountOfItems5() {
|
||||
public void testsize5() {
|
||||
//arrange
|
||||
Stack<String> stack = new Stack<>();
|
||||
PowerStack<String> stack = new PowerStack<>();
|
||||
int expectedCountOfItems = 3;
|
||||
int returnedCountOfItems;
|
||||
//act
|
||||
stack.push("John");
|
||||
stack.pushAndReturn("John");
|
||||
stack.pop();
|
||||
stack.push("Jack").push("Anne").push("Charlie").push("Susan");
|
||||
stack.pushAndReturn("Jack").pushAndReturn("Anne").pushAndReturn("Charlie").pushAndReturn("Susan");
|
||||
|
||||
stack.pop();
|
||||
stack.push("Jane");
|
||||
stack.pushAndReturn("Jane");
|
||||
stack.pop();
|
||||
returnedCountOfItems = stack.getCountOfItems();
|
||||
returnedCountOfItems = stack.size();
|
||||
//assert
|
||||
assertEquals(expectedCountOfItems, returnedCountOfItems);
|
||||
}
|
||||
@ -143,7 +147,7 @@ public class StackTest {
|
||||
@Test
|
||||
public void testIsEmpty() {
|
||||
//arrange
|
||||
Stack<String> stack = new Stack<>();
|
||||
PowerStack<String> stack = new PowerStack<>();
|
||||
boolean expectedValue = true;
|
||||
boolean returnedValue;
|
||||
//act
|
||||
@ -158,18 +162,18 @@ public class StackTest {
|
||||
@Test
|
||||
public void testIsEmpty2() {
|
||||
//arrange
|
||||
Stack<String> stack = new Stack<>();
|
||||
PowerStack<String> stack = new PowerStack<>();
|
||||
boolean expectedValue = true;
|
||||
boolean returnedValue;
|
||||
//act
|
||||
stack.push("John").push("Jack").push("Anne").push("Charlie");
|
||||
stack.pushAndReturn("John").pushAndReturn("Jack").pushAndReturn("Anne").pushAndReturn("Charlie");
|
||||
|
||||
stack.pop();
|
||||
stack.pop();
|
||||
stack.pop();
|
||||
stack.pop();
|
||||
|
||||
stack.push("Jane");
|
||||
stack.pushAndReturn("Jane");
|
||||
stack.pop();
|
||||
returnedValue = stack.isEmpty();
|
||||
//assert
|
||||
@ -182,17 +186,17 @@ public class StackTest {
|
||||
@Test
|
||||
public void testIsEmpty3() {
|
||||
//arrange
|
||||
Stack<String> stack = new Stack<>();
|
||||
PowerStack<String> stack = new PowerStack<>();
|
||||
boolean expectedValue = false;
|
||||
boolean returnedValue;
|
||||
//act
|
||||
stack.push("John").push("Jack").push("Anne").push("Charlie");
|
||||
stack.pushAndReturn("John").pushAndReturn("Jack").pushAndReturn("Anne").pushAndReturn("Charlie");
|
||||
|
||||
stack.pop();
|
||||
stack.pop();
|
||||
stack.pop();
|
||||
|
||||
stack.push("Jane");
|
||||
stack.pushAndReturn("Jane");
|
||||
stack.pop();
|
||||
returnedValue = stack.isEmpty();
|
||||
//assert
|
||||
@ -205,16 +209,16 @@ public class StackTest {
|
||||
@Test
|
||||
public void testIsEmpty4() {
|
||||
//arrange
|
||||
Stack<String> stack = new Stack<>();
|
||||
PowerStack<String> stack = new PowerStack<>();
|
||||
boolean expectedValue = false;
|
||||
boolean returnedValue;
|
||||
//act
|
||||
stack.push("John").push("Jack").push("Anne").push("Charlie");
|
||||
stack.pushAndReturn("John").pushAndReturn("Jack").pushAndReturn("Anne").pushAndReturn("Charlie");
|
||||
|
||||
stack.pop();
|
||||
stack.pop();
|
||||
|
||||
stack.push("Jane");
|
||||
stack.pushAndReturn("Jane");
|
||||
stack.pop();
|
||||
returnedValue = stack.isEmpty();
|
||||
//assert
|
||||
@ -225,18 +229,18 @@ public class StackTest {
|
||||
* Test of push method, of class Stack.
|
||||
*/
|
||||
@Test
|
||||
public void testPush() {
|
||||
public void testpushAndReturn() {
|
||||
//arrange
|
||||
Stack<String> stack = new Stack<>();
|
||||
PowerStack<String> stack = new PowerStack<>();
|
||||
String expectedString = "Jack";
|
||||
String returnedString;
|
||||
//act
|
||||
stack.push("John").push("Jack").push("Anne").push("Charlie");
|
||||
stack.pushAndReturn("John").pushAndReturn("Jack").pushAndReturn("Anne").pushAndReturn("Charlie");
|
||||
|
||||
stack.pop();
|
||||
stack.pop();
|
||||
|
||||
stack.push("Jane");
|
||||
stack.pushAndReturn("Jane");
|
||||
stack.pop();
|
||||
returnedString = stack.pop();
|
||||
//assert
|
||||
@ -249,32 +253,53 @@ public class StackTest {
|
||||
@Test
|
||||
public void testPop() {
|
||||
//arrange
|
||||
Stack<String> stack = new Stack<>();
|
||||
PowerStack<String> stack = new PowerStack<>();
|
||||
String expectedString = "Dennis";
|
||||
String returnedString;
|
||||
//act
|
||||
stack.push("John").push("Jack").push("Anne").push("Charlie").push("Alice").push("Dennis");
|
||||
stack.pushAndReturn("John").pushAndReturn("Jack").pushAndReturn("Anne").pushAndReturn("Charlie").pushAndReturn("Alice").pushAndReturn("Dennis");
|
||||
|
||||
stack.push("Jane");
|
||||
stack.pushAndReturn("Jane");
|
||||
stack.pop();
|
||||
returnedString = stack.pop();
|
||||
//assert
|
||||
assertEquals(expectedString, returnedString);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of pop method, of class Stack.
|
||||
*/
|
||||
@Test
|
||||
public void testPop2() {
|
||||
|
||||
//arrange
|
||||
boolean isExceptionThrown = false;
|
||||
PowerStack<String> stack = new PowerStack<>();
|
||||
//act
|
||||
try {
|
||||
stack.pop();
|
||||
} catch (NoSuchElementException e) {
|
||||
isExceptionThrown = true;
|
||||
}
|
||||
//assert
|
||||
if (!isExceptionThrown) {
|
||||
fail("There should be thrown NoSuchElementException");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of peek method, of class Stack.
|
||||
*/
|
||||
@Test
|
||||
public void testPeek() {
|
||||
//arrange
|
||||
Stack<String> stack = new Stack<>();
|
||||
PowerStack<String> stack = new PowerStack<>();
|
||||
String expectedString = "Dennis";
|
||||
String returnedString;
|
||||
//act
|
||||
stack.push("John").push("Jack").push("Anne").push("Charlie").push("Alice").push("Dennis");
|
||||
stack.pushAndReturn("John").pushAndReturn("Jack").pushAndReturn("Anne").pushAndReturn("Charlie").pushAndReturn("Alice").pushAndReturn("Dennis");
|
||||
|
||||
stack.push("Jane");
|
||||
stack.pushAndReturn("Jane");
|
||||
stack.pop();
|
||||
returnedString = stack.peek();
|
||||
//assert
|
||||
@ -287,38 +312,34 @@ public class StackTest {
|
||||
@Test
|
||||
public void testPeek2() {
|
||||
//arrange
|
||||
Stack<String> stack = new Stack<>();
|
||||
PowerStack<String> stack = new PowerStack<>();
|
||||
int expectedValue = 6;
|
||||
int returnedValue;
|
||||
//act
|
||||
stack.push("John").push("Jack").push("Anne").push("Charlie").push("Alice").push("Dennis");
|
||||
stack.pushAndReturn("John").pushAndReturn("Jack").pushAndReturn("Anne").pushAndReturn("Charlie").pushAndReturn("Alice").pushAndReturn("Dennis");
|
||||
|
||||
stack.push("Jane");
|
||||
stack.pushAndReturn("Jane");
|
||||
stack.pop();
|
||||
returnedValue = stack.getCountOfItems();
|
||||
returnedValue = stack.size();
|
||||
//assert
|
||||
assertEquals(expectedValue, returnedValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of pop method, of class Stack.
|
||||
* Test of peek method, of class Stack.
|
||||
*/
|
||||
@Test
|
||||
public void testPop2() {
|
||||
|
||||
public void testClear() {
|
||||
//arrange
|
||||
boolean isExceptionThrown = false;
|
||||
Stack<String> stack = new Stack<>();
|
||||
PowerStack<String> stack = new PowerStack<>();
|
||||
boolean returnedValue;
|
||||
//act
|
||||
try {
|
||||
stack.pop();
|
||||
} catch (PowerRuntimeException e) {
|
||||
isExceptionThrown = true;
|
||||
}
|
||||
stack.pushAndReturn("John").pushAndReturn("Jack").pushAndReturn("Anne").pushAndReturn("Charlie").pushAndReturn("Alice").pushAndReturn("Dennis");
|
||||
|
||||
stack.clear();
|
||||
returnedValue = stack.isEmpty();
|
||||
//assert
|
||||
if (!isExceptionThrown) {
|
||||
fail("There should be thrown PowerRuntimeException");
|
||||
}
|
||||
assertTrue(returnedValue);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -327,11 +348,11 @@ public class StackTest {
|
||||
@Test
|
||||
public void testToString() {
|
||||
//arrange
|
||||
Stack<String> stack = new Stack<>();
|
||||
String expectedString = "Jane, Alice, ";
|
||||
PowerStack<String> stack = new PowerStack<>();
|
||||
String expectedString = "Jane, Alice";
|
||||
String returnedString;
|
||||
//act
|
||||
stack.push("Alice").push("Jane");
|
||||
stack.pushAndReturn("Alice").pushAndReturn("Jane");
|
||||
returnedString = stack.toString();
|
||||
//assert
|
||||
assertEquals(expectedString, returnedString);
|
57
power-core/pom.xml
Normal file
57
power-core/pom.xml
Normal file
@ -0,0 +1,57 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
power-framework: Java library with many purposes of usage.
|
||||
Copyright (C) 2016-2022 the original author or authors.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation;
|
||||
version 2.1 of the License only.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
-->
|
||||
<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>
|
||||
|
||||
<parent>
|
||||
<groupId>org.nanoboot.powerframework</groupId>
|
||||
<artifactId>power-framework</artifactId>
|
||||
<version>2.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>power-core</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>Power Core</name>
|
||||
<description>Core functionality for the Power library</description>
|
||||
|
||||
<properties>
|
||||
<checkstyle.skip>false</checkstyle.skip>
|
||||
</properties>
|
||||
<build>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
|
||||
</plugins>
|
||||
</build>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
4
power-core/src/lombok.config
Normal file
4
power-core/src/lombok.config
Normal file
@ -0,0 +1,4 @@
|
||||
# tells Lombok that this is the root directory and that it shouldn’t search parent directories for more configuration files
|
||||
config.stopBubbling = true
|
||||
# tells Lombok to add @lombok.Generated annotation to all generated methods
|
||||
lombok.addLombokGeneratedAnnotation = true
|
32
power-core/src/main/java/module-info.java
Normal file
32
power-core/src/main/java/module-info.java
Normal file
@ -0,0 +1,32 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
module powerframework.core {
|
||||
requires lombok;
|
||||
exports org.nanoboot.powerframework.core;
|
||||
exports org.nanoboot.powerframework.core.exceptions;
|
||||
exports org.nanoboot.powerframework.core.version;
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.core;
|
||||
|
||||
/**
|
||||
* Exception class for classes in core package.
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
public class CoreException extends PowerException {
|
||||
|
||||
/**
|
||||
* Constructor with message.
|
||||
*
|
||||
* @param message message describing the reason,
|
||||
* why this exception was thrown.
|
||||
*/
|
||||
public CoreException(final String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.core;
|
||||
|
||||
/**
|
||||
* Exception for Power library.
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
public class PowerException extends RuntimeException {
|
||||
|
||||
/**
|
||||
* Constructor without message.
|
||||
*/
|
||||
public PowerException() {
|
||||
this("No detail.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor with message.
|
||||
*
|
||||
* @param message Message describing, what happened
|
||||
*/
|
||||
public PowerException(final String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor with exception.
|
||||
*
|
||||
* @param exception another exception, which was thrown
|
||||
*/
|
||||
public PowerException(final Exception exception) {
|
||||
this(exception.toString());
|
||||
}
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.core;
|
||||
|
||||
import org.nanoboot.powerframework.core.version.Version;
|
||||
import org.nanoboot.powerframework.core.version.VersionType;
|
||||
|
||||
/**
|
||||
* Power is the core class of the Power library.
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
public final class PowerFramework {
|
||||
|
||||
/**
|
||||
* Current mayor number.
|
||||
*/
|
||||
private static final int MAJOR_NUMBER = 1;
|
||||
/**
|
||||
* Current minor number.
|
||||
*/
|
||||
private static final int MINOR_NUMBER = 0;
|
||||
/**
|
||||
* Current patch number.
|
||||
*/
|
||||
private static final int PATCH_NUMBER = 0;
|
||||
/**
|
||||
* Current version of the Power framework.
|
||||
*/
|
||||
private static Version version =
|
||||
new Version(
|
||||
MAJOR_NUMBER,
|
||||
MINOR_NUMBER,
|
||||
PATCH_NUMBER,
|
||||
VersionType.SNAPSHOT,
|
||||
0);
|
||||
/**
|
||||
* Returns the version number of Power library.
|
||||
*
|
||||
* @return the version number of Power library
|
||||
*/
|
||||
public static String getVersion() {
|
||||
return version.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* Not meant to be instantiated.
|
||||
*/
|
||||
private PowerFramework() {
|
||||
//Not meant to be instantiated.
|
||||
//NOSONAR
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.core;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* Base Power object.
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
public class PowerObject {
|
||||
/**
|
||||
* Next id.
|
||||
*/
|
||||
private static long nextPowerObjectId = 0;
|
||||
/**
|
||||
* Id.
|
||||
*/
|
||||
@Getter
|
||||
private final long powerObjectId;
|
||||
|
||||
/**
|
||||
*Constructor.
|
||||
*/
|
||||
public PowerObject() {
|
||||
this.powerObjectId = nextPowerObjectId;
|
||||
nextPowerObjectId++;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// power-framework: Java library with many purposes of usage.
|
||||
// Copyright (C) 2016-2022 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation;
|
||||
// version 2.1 of the License only.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package org.nanoboot.powerframework.core.exceptions;
|
||||
|
||||
import org.nanoboot.powerframework.core.PowerException;
|
||||
|
||||
/**
|
||||
* Exception happening in case, something is called, but not implemented yet.
|
||||
*
|
||||
* @author <a href="mailto:robertvokac@nanoboot.org">Robert Vokac</a>
|
||||
* @since 0.0.0
|
||||
*/
|
||||
public class NotYetImplementedException extends PowerException {
|
||||
|
||||
/**
|
||||
* Constructor with message.
|
||||
*
|
||||
* @param message message describing the reason,
|
||||
* why this exception was thrown.
|
||||
*/
|
||||
public NotYetImplementedException(final String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user