initial draft
This commit is contained in:
+34
@@ -0,0 +1,34 @@
|
|||||||
|
target/
|
||||||
|
.mvn/
|
||||||
|
mvnw*
|
||||||
|
|
||||||
|
### IntelliJ IDEA ###
|
||||||
|
.idea/
|
||||||
|
*.iws
|
||||||
|
*.iml
|
||||||
|
*.ipr
|
||||||
|
|
||||||
|
### Eclipse ###
|
||||||
|
.apt_generated
|
||||||
|
.classpath
|
||||||
|
.factorypath
|
||||||
|
.project
|
||||||
|
.settings
|
||||||
|
.springBeans
|
||||||
|
.sts4-cache
|
||||||
|
|
||||||
|
### NetBeans ###
|
||||||
|
/nbproject/private/
|
||||||
|
/nbbuild/
|
||||||
|
/dist/
|
||||||
|
/nbdist/
|
||||||
|
/.nb-gradle/
|
||||||
|
build/
|
||||||
|
!**/src/main/**/build/
|
||||||
|
!**/src/test/**/build/
|
||||||
|
|
||||||
|
### VS Code ###
|
||||||
|
.vscode/
|
||||||
|
|
||||||
|
### Mac OS ###
|
||||||
|
.DS_Store
|
||||||
@@ -0,0 +1,159 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<groupId>ru.darktale</groupId>
|
||||||
|
<artifactId>calc</artifactId>
|
||||||
|
<name>calc</name>
|
||||||
|
<version>1.0</version>
|
||||||
|
<build>
|
||||||
|
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
|
||||||
|
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.11.0</version>
|
||||||
|
<configuration>
|
||||||
|
<source>21</source>
|
||||||
|
<target>21</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.openjfx</groupId>
|
||||||
|
<artifactId>javafx-maven-plugin</artifactId>
|
||||||
|
<version>0.0.8</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>default-cli</id>
|
||||||
|
<configuration>
|
||||||
|
<mainClass>ru.darktale.calc.CalcApplication</mainClass>
|
||||||
|
<launcher>app</launcher>
|
||||||
|
<jlinkZipName>app</jlinkZipName>
|
||||||
|
<jlinkImageName>app</jlinkImageName>
|
||||||
|
<noManPages>true</noManPages>
|
||||||
|
<stripDebug>true</stripDebug>
|
||||||
|
<noHeaderFiles>true</noHeaderFiles>
|
||||||
|
<jvmArgs>--add-modules=javafx.controls,javafx.fxml
|
||||||
|
--module-path "C:\Users\mi\.m2\repository\org\openjfx\javafx-fxml\21-ea+24";"C:\Users\mi\.m2\repository\org\openjfx\javafx-controls\21-ea+24";"C:\Users\mi\.m2\repository\org\openjfx\javafx-graphics\21-ea+24";"C:\Users\mi\.m2\repository\org\openjfx\javafx-base\21-ea+24"</jvmArgs>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.jetbrains.kotlin</groupId>
|
||||||
|
<artifactId>kotlin-maven-plugin</artifactId>
|
||||||
|
<version>${kotlin.version}</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>compile</id>
|
||||||
|
<phase>process-sources</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>compile</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
<execution>
|
||||||
|
<id>test-compile</id>
|
||||||
|
<phase>test-compile</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>test-compile</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
<configuration>
|
||||||
|
<jvmTarget>21</jvmTarget>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-shade-plugin</artifactId>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<phase>package</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>shade</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
<profiles>
|
||||||
|
<profile>
|
||||||
|
<id>native</id>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.graalvm.nativeimage</groupId>
|
||||||
|
<artifactId>native-image-maven-plugin</artifactId>
|
||||||
|
<version>19.3.6</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<phase>package</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>native-image</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
<configuration>
|
||||||
|
<imageName>calc-1.0</imageName>
|
||||||
|
<buildArgs>--no-fallback</buildArgs>
|
||||||
|
<mainClass>ru.darktale.calc.CalcApplicationKt</mainClass>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</profile>
|
||||||
|
</profiles>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-api</artifactId>
|
||||||
|
<version>5.9.2</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<artifactId>opentest4j</artifactId>
|
||||||
|
<groupId>org.opentest4j</groupId>
|
||||||
|
</exclusion>
|
||||||
|
<exclusion>
|
||||||
|
<artifactId>junit-platform-commons</artifactId>
|
||||||
|
<groupId>org.junit.platform</groupId>
|
||||||
|
</exclusion>
|
||||||
|
<exclusion>
|
||||||
|
<artifactId>apiguardian-api</artifactId>
|
||||||
|
<groupId>org.apiguardian</groupId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-engine</artifactId>
|
||||||
|
<version>5.9.2</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<artifactId>junit-platform-engine</artifactId>
|
||||||
|
<groupId>org.junit.platform</groupId>
|
||||||
|
</exclusion>
|
||||||
|
<exclusion>
|
||||||
|
<artifactId>apiguardian-api</artifactId>
|
||||||
|
<groupId>org.apiguardian</groupId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
<dependencyManagement>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.guava</groupId>
|
||||||
|
<artifactId>guava</artifactId>
|
||||||
|
<version>33.0.0-jre</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</dependencyManagement>
|
||||||
|
<properties>
|
||||||
|
<kotlin.version>1.9.20</kotlin.version>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<spring.boot.version>3.2.2</spring.boot.version>
|
||||||
|
<junit.version>5.9.2</junit.version>
|
||||||
|
<java.fx.version>21-ea+24</java.fx.version>
|
||||||
|
</properties>
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,207 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>ru.darktale</groupId>
|
||||||
|
<artifactId>calc</artifactId>
|
||||||
|
<version>1.0</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
<name>calc</name>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<junit.version>5.9.2</junit.version>
|
||||||
|
<kotlin.version>1.9.20</kotlin.version>
|
||||||
|
<spring.boot.version>3.2.2</spring.boot.version>
|
||||||
|
<java.fx.version>21-ea+24</java.fx.version>
|
||||||
|
</properties>
|
||||||
|
<dependencyManagement>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.guava</groupId>
|
||||||
|
<artifactId>guava</artifactId>
|
||||||
|
<version>33.0.0-jre</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</dependencyManagement>
|
||||||
|
<dependencies>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.inject</groupId>
|
||||||
|
<artifactId>guice</artifactId>
|
||||||
|
<version>7.0.0</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.openjfx</groupId>
|
||||||
|
<artifactId>javafx-controls</artifactId>
|
||||||
|
<version>${java.fx.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.openjfx</groupId>
|
||||||
|
<artifactId>javafx-fxml</artifactId>
|
||||||
|
<version>${java.fx.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jetbrains.kotlin</groupId>
|
||||||
|
<artifactId>kotlin-stdlib</artifactId>
|
||||||
|
<version>${kotlin.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- test -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-api</artifactId>
|
||||||
|
<version>${junit.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-engine</artifactId>
|
||||||
|
<version>${junit.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-params</artifactId>
|
||||||
|
<version>${junit.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.mockk</groupId>
|
||||||
|
<artifactId>mockk-jvm</artifactId>
|
||||||
|
<version>1.13.10</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jetbrains.kotlin</groupId>
|
||||||
|
<artifactId>kotlin-test-junit5</artifactId>
|
||||||
|
<version>1.8.22</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<!-- test -->
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
|
||||||
|
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.11.0</version>
|
||||||
|
<configuration>
|
||||||
|
<source>21</source>
|
||||||
|
<target>21</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin> <!--
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-jar-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<archive>
|
||||||
|
<manifest>
|
||||||
|
<addClasspath>true</addClasspath>
|
||||||
|
<classpathPrefix>lib/</classpathPrefix>
|
||||||
|
<mainClass>ru.darktale.calc.CalcApplicationKt</mainClass>
|
||||||
|
</manifest>
|
||||||
|
</archive>
|
||||||
|
</configuration>
|
||||||
|
</plugin> -->
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.openjfx</groupId>
|
||||||
|
<artifactId>javafx-maven-plugin</artifactId>
|
||||||
|
<version>0.0.8</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<!-- Default configuration for running with: mvn clean javafx:run -->
|
||||||
|
<id>default-cli</id>
|
||||||
|
<configuration>
|
||||||
|
<mainClass>ru.darktale.calc.CalcApplication</mainClass>
|
||||||
|
<launcher>app</launcher>
|
||||||
|
<jlinkZipName>app</jlinkZipName>
|
||||||
|
<jlinkImageName>app</jlinkImageName>
|
||||||
|
<noManPages>true</noManPages>
|
||||||
|
<stripDebug>true</stripDebug>
|
||||||
|
<noHeaderFiles>true</noHeaderFiles>
|
||||||
|
<jvmArgs>
|
||||||
|
--add-modules=javafx.controls,javafx.fxml
|
||||||
|
--module-path "C:\Users\mi\.m2\repository\org\openjfx\javafx-fxml\21-ea+24";"C:\Users\mi\.m2\repository\org\openjfx\javafx-controls\21-ea+24";"C:\Users\mi\.m2\repository\org\openjfx\javafx-graphics\21-ea+24";"C:\Users\mi\.m2\repository\org\openjfx\javafx-base\21-ea+24"
|
||||||
|
</jvmArgs>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.jetbrains.kotlin</groupId>
|
||||||
|
<artifactId>kotlin-maven-plugin</artifactId>
|
||||||
|
<version>${kotlin.version}</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>compile</id>
|
||||||
|
<phase>process-sources</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>compile</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
<execution>
|
||||||
|
<id>test-compile</id>
|
||||||
|
<phase>test-compile</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>test-compile</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
<configuration>
|
||||||
|
<jvmTarget>21</jvmTarget>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-shade-plugin</artifactId>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<phase>package</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>shade</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
<profiles>
|
||||||
|
<profile>
|
||||||
|
<id>native</id>
|
||||||
|
<activation>
|
||||||
|
<activeByDefault>false</activeByDefault>
|
||||||
|
</activation>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.graalvm.nativeimage</groupId>
|
||||||
|
<artifactId>native-image-maven-plugin</artifactId>
|
||||||
|
<version>19.3.6</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<goals>
|
||||||
|
<goal>native-image</goal>
|
||||||
|
</goals>
|
||||||
|
<phase>package</phase>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
<configuration>
|
||||||
|
<imageName>calc-1.0</imageName>
|
||||||
|
<buildArgs>
|
||||||
|
<!--no-fallback-->
|
||||||
|
</buildArgs>
|
||||||
|
<mainClass>ru.darktale.calc.CalcApplicationKt</mainClass>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</profile>
|
||||||
|
</profiles>
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
package ru.darktale.calc
|
||||||
|
|
||||||
|
import com.google.inject.Guice
|
||||||
|
import javafx.application.Application
|
||||||
|
import javafx.fxml.FXMLLoader
|
||||||
|
import javafx.scene.Scene
|
||||||
|
import javafx.stage.Stage
|
||||||
|
import ru.darktale.calc.infra.BasicModule
|
||||||
|
import ru.darktale.calc.infra.CalculationsModule
|
||||||
|
import ru.darktale.calc.service.KeyEventHandler
|
||||||
|
import kotlin.system.measureNanoTime
|
||||||
|
import kotlin.system.measureTimeMillis
|
||||||
|
|
||||||
|
|
||||||
|
class CalcApplication : Application() {
|
||||||
|
override fun start(stage: Stage) {
|
||||||
|
initScene(stage)
|
||||||
|
stage.title = "Calc!"
|
||||||
|
stage.show()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun initScene(stage: Stage) {
|
||||||
|
val injector = Guice.createInjector(BasicModule())
|
||||||
|
val fxmlLoader = FXMLLoader(CalcApplication::class.java.getResource("hello-view.fxml"))
|
||||||
|
fxmlLoader.setControllerFactory { injector.getInstance(it) }
|
||||||
|
val scene = Scene(fxmlLoader.load(), 320.0, 240.0)
|
||||||
|
scene.onKeyPressed = injector.getInstance(KeyEventHandler::class.java)
|
||||||
|
stage.scene = scene
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
fun main() {
|
||||||
|
Application.launch(CalcApplication::class.java)
|
||||||
|
}
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
fun main() {
|
||||||
|
val injector = Guice.createInjector(CalculationsModule())
|
||||||
|
val parser = injector.getInstance(Parser::class.java)
|
||||||
|
val calculator = injector.getInstance(Calculator::class.java)
|
||||||
|
|
||||||
|
val expression = "9^2"
|
||||||
|
val time = measureTimeMillis {
|
||||||
|
println("$expression = ${calculator.calculate(parser.parse(expression))}")
|
||||||
|
}
|
||||||
|
println("$time ms elapsed")
|
||||||
|
}
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
package ru.darktale.calc
|
||||||
|
|
||||||
|
import jakarta.inject.Inject
|
||||||
|
import jakarta.inject.Singleton
|
||||||
|
import javafx.fxml.FXML
|
||||||
|
import javafx.scene.control.TextField
|
||||||
|
import ru.darktale.calc.service.CalculatorService
|
||||||
|
|
||||||
|
@Singleton
|
||||||
|
class CalcController {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private lateinit var calculatorService: CalculatorService
|
||||||
|
|
||||||
|
@FXML
|
||||||
|
private lateinit var calcField: TextField
|
||||||
|
|
||||||
|
@FXML
|
||||||
|
private fun on1Click() = addSymbol('1')
|
||||||
|
@FXML
|
||||||
|
private fun on2Click() = addSymbol('2')
|
||||||
|
@FXML
|
||||||
|
private fun on3Click() = addSymbol('3')
|
||||||
|
@FXML
|
||||||
|
private fun on4Click() = addSymbol('4')
|
||||||
|
@FXML
|
||||||
|
private fun on5Click() = addSymbol('5')
|
||||||
|
@FXML
|
||||||
|
private fun on6Click() = addSymbol('6')
|
||||||
|
@FXML
|
||||||
|
private fun on7Click() = addSymbol('7')
|
||||||
|
@FXML
|
||||||
|
private fun on8Click() = addSymbol('8')
|
||||||
|
@FXML
|
||||||
|
private fun on9Click() = addSymbol('9')
|
||||||
|
@FXML
|
||||||
|
private fun on0Click() = addSymbol('0')
|
||||||
|
@FXML
|
||||||
|
private fun onDotClick() = addSymbol('.')
|
||||||
|
|
||||||
|
fun addSymbol(symbol: Char) {
|
||||||
|
println(this)
|
||||||
|
calcField.text = calculatorService.addSymbol(symbol)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun removeLast() {
|
||||||
|
println(this)
|
||||||
|
calcField.text = calculatorService.removeLast()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package ru.darktale.calc
|
||||||
|
|
||||||
|
import jakarta.inject.Inject
|
||||||
|
import java.math.BigDecimal
|
||||||
|
import kotlin.collections.ArrayDeque
|
||||||
|
|
||||||
|
interface Calculator {
|
||||||
|
fun calculate(expression: Expression): BigDecimal
|
||||||
|
}
|
||||||
|
|
||||||
|
class CalculatorImpl @Inject constructor(
|
||||||
|
private val converter: ExpressionToRplConverter,
|
||||||
|
private val unaryFunctions: Map<UnaryOperation, UnaryOperationFunction<out UnaryOperation>>,
|
||||||
|
private val binaryFunctions: Map<BinaryOperation, BinaryOperationFunction<out BinaryOperation>>,
|
||||||
|
) : Calculator {
|
||||||
|
|
||||||
|
override fun calculate(expression: Expression): BigDecimal {
|
||||||
|
val operands = ArrayDeque<Operand>()
|
||||||
|
converter.convert(expression).process(operands)
|
||||||
|
return if (operands.size == 1)
|
||||||
|
operands.removeFirst().toBigDecimal()
|
||||||
|
else throw CalculationException()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun List<CalculationToken>.process(operands: ArrayDeque<Operand>) = forEach {
|
||||||
|
when (it) {
|
||||||
|
is Operand -> operands.addFirst(it)
|
||||||
|
is BinaryOperation -> {
|
||||||
|
val second = operands.removeFirst()
|
||||||
|
operands.addFirst(binaryFunctions[it]!!.apply(operands.removeFirst(), second))
|
||||||
|
}
|
||||||
|
is UnaryOperation -> operands.addFirst(unaryFunctions[it]!!.apply(operands.removeFirst()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class CalculationException : Exception()
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
package ru.darktale.calc
|
||||||
|
|
||||||
|
interface ExpressionToRplConverter {
|
||||||
|
fun convert(expression: Expression): List<CalculationToken>
|
||||||
|
}
|
||||||
|
|
||||||
|
class ExpressionToRplConverterImpl : ExpressionToRplConverter {
|
||||||
|
override fun convert(expression: Expression) = expression.toRpl()
|
||||||
|
|
||||||
|
/** RPL - reverse polish notation */
|
||||||
|
private fun Expression.toRpl() = mutableListOf<CalculationToken>().apply {
|
||||||
|
val operationStack = ArrayDeque<ExpressionElement>()
|
||||||
|
val queue = ArrayDeque(elements)
|
||||||
|
|
||||||
|
while (queue.isNotEmpty()) {
|
||||||
|
when (val element = queue.removeFirst()) {
|
||||||
|
is Operand -> {
|
||||||
|
this += element
|
||||||
|
if (operationStack.firstOrNull() is UnaryOperation) this += operationStack.removeFirst() as UnaryOperation
|
||||||
|
}
|
||||||
|
|
||||||
|
is Expression -> queue += element.elements.toTypedArray()
|
||||||
|
OpenBracket -> operationStack.addFirst(element)
|
||||||
|
is UnaryOperation -> {
|
||||||
|
if (element.isPrefix) operationStack.addFirst(element)
|
||||||
|
else this += element
|
||||||
|
}
|
||||||
|
|
||||||
|
CloseBracket -> {
|
||||||
|
// pass elements to RPL
|
||||||
|
while (operationStack.first() != OpenBracket) this += operationStack.removeFirst() as CalculationToken
|
||||||
|
//remove OpenBracket itself
|
||||||
|
operationStack.removeFirst()
|
||||||
|
}
|
||||||
|
|
||||||
|
is BinaryOperation -> {
|
||||||
|
while (
|
||||||
|
operationStack.firstOrNull()?.let {
|
||||||
|
it is BinaryOperation &&
|
||||||
|
it.priority <= element.priority
|
||||||
|
} == true
|
||||||
|
) {
|
||||||
|
this += operationStack.removeFirst() as CalculationToken
|
||||||
|
}
|
||||||
|
operationStack.addFirst(element)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while (operationStack.isNotEmpty()) {
|
||||||
|
this += operationStack.removeFirst() as CalculationToken
|
||||||
|
}
|
||||||
|
} as List<CalculationToken>
|
||||||
|
}
|
||||||
@@ -0,0 +1,139 @@
|
|||||||
|
package ru.darktale.calc
|
||||||
|
|
||||||
|
import java.math.BigDecimal
|
||||||
|
|
||||||
|
sealed class ExpressionElement {
|
||||||
|
abstract val elementType: ElementType
|
||||||
|
}
|
||||||
|
|
||||||
|
interface CalculationToken
|
||||||
|
|
||||||
|
class Operand(
|
||||||
|
private val value: String
|
||||||
|
) : ExpressionElement(), CalculationToken {
|
||||||
|
override var elementType: ElementType = ElementType.NUMBER
|
||||||
|
|
||||||
|
private lateinit var bigDecimalValue: BigDecimal
|
||||||
|
|
||||||
|
override fun toString() = value
|
||||||
|
fun toBigDecimal(): BigDecimal {
|
||||||
|
if (!::bigDecimalValue.isInitialized) bigDecimalValue = value.toBigDecimal()
|
||||||
|
return bigDecimalValue
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun equals(other: Any?): Boolean {
|
||||||
|
if (other !is Operand) return false
|
||||||
|
return value == other.value ||
|
||||||
|
toBigDecimal() == other.toBigDecimal()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun hashCode(): Int {
|
||||||
|
return toBigDecimal().hashCode()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
open class Expression(
|
||||||
|
val elements: List<ExpressionElement>,
|
||||||
|
) : ExpressionElement() {
|
||||||
|
constructor(vararg elements: ExpressionElement) : this(elements.toList())
|
||||||
|
|
||||||
|
override val elementType = ElementType.EXPRESSION
|
||||||
|
override fun toString() = elements.joinToString("")
|
||||||
|
}
|
||||||
|
|
||||||
|
class BracketExpression(elements: List<ExpressionElement>) : Expression(listOf(OpenBracket) + elements + CloseBracket)
|
||||||
|
|
||||||
|
sealed class Bracket(private val value: String) : ExpressionElement() {
|
||||||
|
override val elementType = ElementType.BRACKET
|
||||||
|
override fun toString() = value
|
||||||
|
}
|
||||||
|
|
||||||
|
object OpenBracket : Bracket("(")
|
||||||
|
object CloseBracket : Bracket(")")
|
||||||
|
|
||||||
|
sealed class OperationElement : ExpressionElement() {
|
||||||
|
abstract val value: String
|
||||||
|
abstract val isUnary: Boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
sealed class UnaryOperation(
|
||||||
|
override val value: String,
|
||||||
|
val isPrefix: Boolean
|
||||||
|
) : OperationElement(), CalculationToken {
|
||||||
|
override val elementType = ElementType.UNARY_OPERATION
|
||||||
|
override val isUnary = true
|
||||||
|
override fun toString() = value
|
||||||
|
}
|
||||||
|
|
||||||
|
object UnaryMinus : UnaryOperation("-", true)
|
||||||
|
object UnaryPlus : UnaryOperation("+", true)
|
||||||
|
object Sinus : UnaryOperation("sin", true)
|
||||||
|
object Sqrt : UnaryOperation("sqrt", true)
|
||||||
|
|
||||||
|
sealed class BinaryOperation(
|
||||||
|
override val value: String,
|
||||||
|
val priority: Int,
|
||||||
|
) : OperationElement(), CalculationToken {
|
||||||
|
override val elementType = ElementType.BINARY_OPERATION
|
||||||
|
override val isUnary = false
|
||||||
|
override fun toString() = value
|
||||||
|
}
|
||||||
|
|
||||||
|
object BinaryMinus : BinaryOperation("-", 3)
|
||||||
|
object BinaryPlus : BinaryOperation("+", 3)
|
||||||
|
object Multiplication : BinaryOperation("*", 2)
|
||||||
|
object Division : BinaryOperation("/", 2)
|
||||||
|
object Power : BinaryOperation("^", 1)
|
||||||
|
|
||||||
|
enum class ElementType {
|
||||||
|
NUMBER,
|
||||||
|
BRACKET,
|
||||||
|
UNARY_OPERATION,
|
||||||
|
BINARY_OPERATION,
|
||||||
|
EXPRESSION,
|
||||||
|
}
|
||||||
|
|
||||||
|
interface OperationMapper<T> {
|
||||||
|
val typeMap: Map<String, T>
|
||||||
|
operator fun get(key: String): OperationElement?
|
||||||
|
}
|
||||||
|
|
||||||
|
enum class UnaryOperations(
|
||||||
|
var operationElement: UnaryOperation?
|
||||||
|
) {
|
||||||
|
UNARY_MINUS(UnaryMinus),
|
||||||
|
UNARY_PLUS(UnaryPlus),
|
||||||
|
SQRT(Sqrt),
|
||||||
|
SINUS(Sinus);
|
||||||
|
|
||||||
|
companion object : OperationMapper<UnaryOperations> {
|
||||||
|
override val typeMap = entries.associateBy { it.operationElement.toString() }
|
||||||
|
override fun get(key: String) = typeMap[key]?.operationElement
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum class BinaryOperations(
|
||||||
|
val operationElement: BinaryOperation,
|
||||||
|
) {
|
||||||
|
BINARY_MINUS(BinaryMinus),
|
||||||
|
BINARY_PLUS(BinaryPlus),
|
||||||
|
MULTIPLICATION(Multiplication),
|
||||||
|
POWER(Power),
|
||||||
|
DIVISION(Division);
|
||||||
|
|
||||||
|
companion object : OperationMapper<BinaryOperations> {
|
||||||
|
override val typeMap = entries.associateBy { it.operationElement.toString() }
|
||||||
|
override fun get(key: String) = typeMap[key]?.operationElement
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum class Brackets(
|
||||||
|
val bracket: Bracket,
|
||||||
|
) {
|
||||||
|
OPEN_BRACKET(OpenBracket),
|
||||||
|
CLOSE_BRACKET(CloseBracket);
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
val typeMap = entries.associateBy { it.bracket.toString() }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
package ru.darktale.calc
|
||||||
|
|
||||||
|
import java.math.BigDecimal
|
||||||
|
import java.math.MathContext.DECIMAL128
|
||||||
|
import java.util.function.BiFunction
|
||||||
|
import java.util.function.Function
|
||||||
|
import kotlin.math.sin
|
||||||
|
|
||||||
|
interface UnaryOperationFunction<T: UnaryOperation> : Function<Operand, Operand>
|
||||||
|
interface BinaryOperationFunction<T: BinaryOperation> : BiFunction<Operand, Operand, Operand>
|
||||||
|
|
||||||
|
object UnaryMinusFunction : UnaryOperationFunction<UnaryMinus> {
|
||||||
|
override fun apply(operand: Operand) = operand.toBigDecimal().unaryMinus().toNumber()
|
||||||
|
}
|
||||||
|
|
||||||
|
object UnaryPlusFunction : UnaryOperationFunction<UnaryPlus> {
|
||||||
|
override fun apply(operand: Operand) = operand
|
||||||
|
}
|
||||||
|
|
||||||
|
object SinusFunction : UnaryOperationFunction<Sinus> {
|
||||||
|
override fun apply(operand: Operand) = sin(operand.toBigDecimal().toDouble()).toNumber()
|
||||||
|
}
|
||||||
|
|
||||||
|
object SqrtFunction : UnaryOperationFunction<Sqrt> {
|
||||||
|
override fun apply(operand: Operand) = operand.toBigDecimal().sqrt(DECIMAL128).toNumber()
|
||||||
|
}
|
||||||
|
|
||||||
|
object BinaryMinusFunction : BinaryOperationFunction<BinaryMinus> {
|
||||||
|
override fun apply(first: Operand, second: Operand) =
|
||||||
|
(first.toBigDecimal() - second.toBigDecimal()).toNumber()
|
||||||
|
}
|
||||||
|
|
||||||
|
object BinaryPlusFunction : BinaryOperationFunction<BinaryPlus> {
|
||||||
|
override fun apply(first: Operand, second: Operand) =
|
||||||
|
(first.toBigDecimal() + second.toBigDecimal()).toNumber()
|
||||||
|
}
|
||||||
|
object MultiplicationFunction : BinaryOperationFunction<Multiplication> {
|
||||||
|
override fun apply(first: Operand, second: Operand) =
|
||||||
|
(first.toBigDecimal() * second.toBigDecimal()).toNumber()
|
||||||
|
}
|
||||||
|
|
||||||
|
object DivisionFunction : BinaryOperationFunction<Division> {
|
||||||
|
override fun apply(first: Operand, second: Operand) =
|
||||||
|
(first.toBigDecimal() / second.toBigDecimal()).toNumber()
|
||||||
|
}
|
||||||
|
|
||||||
|
object PowerFunction : BinaryOperationFunction<Power> {
|
||||||
|
override fun apply(first: Operand, second: Operand) =
|
||||||
|
(first.toBigDecimal().pow(second.toBigDecimal().toInt())).toNumber()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun BigDecimal.toNumber() = Operand(toString())
|
||||||
|
fun Double.toNumber() = Operand(toString())
|
||||||
@@ -0,0 +1,160 @@
|
|||||||
|
package ru.darktale.calc
|
||||||
|
|
||||||
|
import jakarta.inject.Inject
|
||||||
|
|
||||||
|
interface Parser {
|
||||||
|
fun parse(expression: String): Expression
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
class ParserImpl : Parser {
|
||||||
|
private val operations = UnaryOperations.typeMap.keys + BinaryOperations.typeMap.keys
|
||||||
|
|
||||||
|
override fun parse(expression: String): Expression {
|
||||||
|
val result = mutableListOf<ExpressionElement>()
|
||||||
|
var (tokenStart, tokenEnd) = 0 to 0
|
||||||
|
var prevNumber: String? = null
|
||||||
|
|
||||||
|
fun parseTokenOrNull(token: String): ExpressionElement? =
|
||||||
|
Brackets.typeMap[token]?.bracket
|
||||||
|
?: if (token in operations) getOperation(result.lastOrNull(), token)
|
||||||
|
else (token.toBigDecimalOrNull() ?: "${token}0".toBigDecimalOrNull())?.let {
|
||||||
|
prevNumber = token
|
||||||
|
return null
|
||||||
|
} ?: prevNumber?.let {
|
||||||
|
Number(it)
|
||||||
|
}
|
||||||
|
|
||||||
|
while (tokenEnd < expression.length) {
|
||||||
|
parseTokenOrNull(expression.substring(tokenStart..tokenEnd))?.let {
|
||||||
|
result += it
|
||||||
|
tokenStart = tokenEnd
|
||||||
|
if (it.elementType == ElementType.NUMBER)
|
||||||
|
tokenEnd--
|
||||||
|
else
|
||||||
|
tokenStart++
|
||||||
|
}
|
||||||
|
tokenEnd++
|
||||||
|
}
|
||||||
|
if (tokenStart < expression.length) {
|
||||||
|
result += Number(expression.substring(tokenStart))
|
||||||
|
}
|
||||||
|
return Expression(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getOperation(previousElement: ExpressionElement?, token: String) = when {
|
||||||
|
previousElement?.elementType == ElementType.NUMBER ||
|
||||||
|
previousElement?.elementType == ElementType.BRACKET ->
|
||||||
|
BinaryOperations.typeMap[token]!!.operationElement
|
||||||
|
|
||||||
|
else -> UnaryOperations.typeMap[token]!!.operationElement
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
class NestedExpressionsParser @Inject constructor(
|
||||||
|
private val unaryOperations: Map<String, UnaryOperation>,
|
||||||
|
private val binaryOperations: Map<String, BinaryOperation>,
|
||||||
|
) : Parser {
|
||||||
|
|
||||||
|
override fun parse(expression: String): Expression {
|
||||||
|
val stack = ArrayDeque<MutableList<ExpressionElement>>()
|
||||||
|
var elements = mutableListOf<ExpressionElement>()
|
||||||
|
var position = 0
|
||||||
|
while (position < expression.length) {
|
||||||
|
Brackets.typeMap[expression.substring(position..position)]?.let { bracket ->
|
||||||
|
if (bracket == Brackets.OPEN_BRACKET) {
|
||||||
|
elements.validateOpenBracket(position)
|
||||||
|
stack.addFirst(elements)
|
||||||
|
elements = mutableListOf()
|
||||||
|
} else {
|
||||||
|
elements.validateCloseBracket(position)
|
||||||
|
val nestedExpression = BracketExpression(elements)
|
||||||
|
elements = stack.removeFirstOrNull() ?: throw ParseException("Unexpected close bracket", position)
|
||||||
|
elements += nestedExpression
|
||||||
|
}
|
||||||
|
position++
|
||||||
|
} ?: run {
|
||||||
|
elements += if (expression[position] in NUMBERS) {
|
||||||
|
elements.validateNumber(position)
|
||||||
|
getNumberLength(expression, position).let {
|
||||||
|
position += it
|
||||||
|
Operand(expression.substring(position - it until position))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
val operations =
|
||||||
|
if (elements.getOperationType(position) == ElementType.UNARY_OPERATION) UnaryOperations
|
||||||
|
else BinaryOperations
|
||||||
|
val key = getOperationKey(operations.typeMap.keys, expression, position)
|
||||||
|
position += (key.length)
|
||||||
|
operations[key]!!
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (stack.isNotEmpty()) throw ParseException("Unexpected end of expression, unclosed bracket")
|
||||||
|
return when {
|
||||||
|
elements.isEmpty() -> EMPTY_EXPRESSION
|
||||||
|
elements.size == 1 && elements[0] is Expression -> elements[0] as Expression
|
||||||
|
else -> Expression(elements)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun List<ExpressionElement>.validateNumber(position: Int) {
|
||||||
|
when (this.lastOrNull()) {
|
||||||
|
is Operand -> throw ParseException("Unexpected number after number", position)
|
||||||
|
is Expression -> throw ParseException("Unexpected number after expression", position)
|
||||||
|
else -> Unit
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun List<ExpressionElement>.validateOpenBracket(position: Int) {
|
||||||
|
when (this.lastOrNull()) {
|
||||||
|
is OperationElement, null -> Unit
|
||||||
|
else -> throw ParseException("Unexpected open bracket", position)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun List<ExpressionElement>.validateCloseBracket(position: Int) {
|
||||||
|
when (this.lastOrNull()) {
|
||||||
|
is Operand, is Expression, Brackets.CLOSE_BRACKET.bracket -> Unit
|
||||||
|
else -> throw ParseException("Unexpected close bracket", position)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun List<ExpressionElement>.getOperationType(position: Int) =
|
||||||
|
when (this.lastOrNull()) {
|
||||||
|
is Operand, is Expression -> ElementType.BINARY_OPERATION
|
||||||
|
is UnaryOperation -> throw ParseException("Not a number after unary operation", position)
|
||||||
|
else -> ElementType.UNARY_OPERATION
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getOperationKey(keys: Set<String>, expression: String, position: Int): String {
|
||||||
|
val maxLength = keys.maxBy { it.length }.length
|
||||||
|
var length = 0
|
||||||
|
while (length < maxLength && position + length <= expression.length &&
|
||||||
|
expression.substring(position until position + length) !in keys
|
||||||
|
) length++
|
||||||
|
if (position + length >= expression.length
|
||||||
|
|| expression.substring(position until position + length) !in keys
|
||||||
|
)
|
||||||
|
throw ParseException("Unknown operation", position)
|
||||||
|
return expression.substring(position until position + length)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getNumberLength(expression: String, start: Int): Int {
|
||||||
|
var end = start
|
||||||
|
while (end < expression.length &&
|
||||||
|
(expression.substring(start..end).toBigDecimalOrNull() != null ||
|
||||||
|
"${expression.substring(start..end)}0".toBigDecimalOrNull() != null)
|
||||||
|
) end++
|
||||||
|
return end - start
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private val NUMBERS = setOf('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class ParseException(message: String, position: Int? = null) :
|
||||||
|
Throwable("""$message${position?.let { " at $it" } ?: ""}""")
|
||||||
|
|
||||||
|
private val EMPTY_EXPRESSION = Expression(emptyList())
|
||||||
|
|
||||||
@@ -0,0 +1,80 @@
|
|||||||
|
package ru.darktale.calc.infra
|
||||||
|
|
||||||
|
import com.google.inject.AbstractModule
|
||||||
|
import com.google.inject.TypeLiteral
|
||||||
|
import com.google.inject.multibindings.MapBinder
|
||||||
|
import com.google.inject.multibindings.Multibinder
|
||||||
|
import ru.darktale.calc.*
|
||||||
|
import ru.darktale.calc.service.CalculatorService
|
||||||
|
import ru.darktale.calc.service.CalculatorServiceImpl
|
||||||
|
import ru.darktale.calc.service.KeyEventHandler
|
||||||
|
import ru.darktale.calc.service.KeyEventHandlerImpl
|
||||||
|
|
||||||
|
class BasicModule : AbstractModule() {
|
||||||
|
override fun configure() {
|
||||||
|
bind(CalculatorService::class.java).to(CalculatorServiceImpl::class.java)
|
||||||
|
bind(KeyEventHandler::class.java).to(KeyEventHandlerImpl::class.java)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class CalculationsModule : AbstractModule() {
|
||||||
|
private val unaryOperationMap = mapOf(
|
||||||
|
UnaryMinus to UnaryMinusFunction,
|
||||||
|
UnaryPlus to UnaryPlusFunction,
|
||||||
|
Sinus to SinusFunction,
|
||||||
|
Sqrt to SqrtFunction,
|
||||||
|
)
|
||||||
|
private val binaryOperationMap = mapOf(
|
||||||
|
BinaryMinus to BinaryMinusFunction,
|
||||||
|
BinaryPlus to BinaryPlusFunction,
|
||||||
|
Multiplication to MultiplicationFunction,
|
||||||
|
Division to DivisionFunction,
|
||||||
|
Power to PowerFunction,
|
||||||
|
)
|
||||||
|
|
||||||
|
override fun configure() {
|
||||||
|
val binder = binder()
|
||||||
|
|
||||||
|
MapBinder.newMapBinder(
|
||||||
|
binder,
|
||||||
|
TypeLiteral.get(UnaryOperation::class.java),
|
||||||
|
object : TypeLiteral<UnaryOperationFunction<out UnaryOperation>>() {},
|
||||||
|
).apply {
|
||||||
|
unaryOperationMap.entries.forEach {
|
||||||
|
addBinding(it.key).toInstance(it.value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
MapBinder.newMapBinder(binder, String::class.java, UnaryOperation::class.java).apply {
|
||||||
|
unaryOperationMap.keys.forEach {
|
||||||
|
addBinding(it.value).toInstance(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
MapBinder.newMapBinder(
|
||||||
|
binder,
|
||||||
|
TypeLiteral.get(BinaryOperation::class.java),
|
||||||
|
object : TypeLiteral<BinaryOperationFunction<out BinaryOperation>>() {},
|
||||||
|
).apply {
|
||||||
|
binaryOperationMap.entries.forEach {
|
||||||
|
addBinding(it.key).toInstance(it.value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
MapBinder.newMapBinder(binder, String::class.java, BinaryOperation::class.java).apply {
|
||||||
|
binaryOperationMap.keys.forEach {
|
||||||
|
addBinding(it.value).toInstance(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Multibinder.newSetBinder(binder, BinaryOperation::class.java).apply {
|
||||||
|
binaryOperationMap.keys.forEach {
|
||||||
|
addBinding().toInstance(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bind(ExpressionToRplConverter::class.java).to(ExpressionToRplConverterImpl::class.java)
|
||||||
|
bind(Calculator::class.java).to(CalculatorImpl::class.java)
|
||||||
|
bind(Parser::class.java).to(NestedExpressionsParser::class.java)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package ru.darktale.calc.service
|
||||||
|
|
||||||
|
import jakarta.inject.Singleton
|
||||||
|
import kotlin.text.StringBuilder
|
||||||
|
|
||||||
|
interface CalculatorService {
|
||||||
|
fun addSymbol(symbol: Char): String
|
||||||
|
fun removeLast(): String
|
||||||
|
}
|
||||||
|
|
||||||
|
@Singleton
|
||||||
|
class CalculatorServiceImpl: CalculatorService {
|
||||||
|
private val builder = StringBuilder()
|
||||||
|
|
||||||
|
override fun addSymbol(symbol: Char): String {
|
||||||
|
builder.append(symbol)
|
||||||
|
return builder.toString()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun removeLast(): String {
|
||||||
|
val len = builder.length
|
||||||
|
return if (len > 0) builder.deleteAt(len - 1).toString() else ""
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package ru.darktale.calc.service
|
||||||
|
|
||||||
|
import jakarta.inject.Inject
|
||||||
|
import javafx.event.EventHandler
|
||||||
|
import javafx.scene.input.KeyCode
|
||||||
|
import javafx.scene.input.KeyEvent
|
||||||
|
import ru.darktale.calc.CalcController
|
||||||
|
import java.awt.Event.BACK_SPACE
|
||||||
|
|
||||||
|
interface KeyEventHandler : EventHandler<KeyEvent>
|
||||||
|
|
||||||
|
class KeyEventHandlerImpl : KeyEventHandler {
|
||||||
|
@Inject
|
||||||
|
private lateinit var calcController: CalcController
|
||||||
|
|
||||||
|
override fun handle(keyEvent: KeyEvent) {
|
||||||
|
println(keyEvent)
|
||||||
|
if (keyEvent.code == KeyCode.BACK_SPACE) calcController.removeLast()
|
||||||
|
else calcController.addSymbol(keyEvent.character.toCharArray()[0])
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
|
<?import javafx.geometry.Insets?>
|
||||||
|
<?import javafx.scene.layout.VBox?>
|
||||||
|
|
||||||
|
<?import javafx.scene.control.Button?>
|
||||||
|
|
||||||
|
<?import javafx.scene.layout.HBox?>
|
||||||
|
<?import javafx.scene.control.TextField?>
|
||||||
|
<VBox alignment="CENTER" spacing="20.0" xmlns:fx="http://javafx.com/fxml"
|
||||||
|
fx:controller="ru.darktale.calc.CalcController">
|
||||||
|
<padding>
|
||||||
|
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0"/>
|
||||||
|
</padding>
|
||||||
|
<HBox>
|
||||||
|
<TextField minWidth="300" fx:id="calcField" editable="false"/>
|
||||||
|
</HBox>
|
||||||
|
<HBox>
|
||||||
|
<Button text="1" onAction="#on1Click"/>
|
||||||
|
<Button text="2" onAction="#on2Click"/>
|
||||||
|
<Button text="3" onAction="#on3Click"/>
|
||||||
|
</HBox>
|
||||||
|
<HBox>
|
||||||
|
<Button text="4" onAction="#on4Click"/>
|
||||||
|
<Button text="5" onAction="#on5Click"/>
|
||||||
|
<Button text="6" onAction="#on6Click"/>
|
||||||
|
</HBox>
|
||||||
|
<HBox>
|
||||||
|
<Button text="7" onAction="#on7Click"/>
|
||||||
|
<Button text="8" onAction="#on8Click"/>
|
||||||
|
<Button text="9" onAction="#on9Click"/>
|
||||||
|
</HBox>
|
||||||
|
<HBox>
|
||||||
|
<Button text="0" minWidth="50" onAction="#on0Click"/>
|
||||||
|
<Button text="." onAction="#onDotClick"/>
|
||||||
|
</HBox>
|
||||||
|
</VBox>
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
package ru.darktale.calc
|
||||||
|
|
||||||
|
import io.mockk.every
|
||||||
|
import io.mockk.impl.annotations.InjectMockKs
|
||||||
|
import io.mockk.impl.annotations.MockK
|
||||||
|
import io.mockk.junit5.MockKExtension
|
||||||
|
import io.mockk.mockk
|
||||||
|
import io.mockk.verify
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
import org.junit.jupiter.api.assertDoesNotThrow
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith
|
||||||
|
import java.math.BigDecimal
|
||||||
|
import kotlin.test.assertEquals
|
||||||
|
|
||||||
|
@ExtendWith(MockKExtension::class)
|
||||||
|
class CalculatorImplTest {
|
||||||
|
@MockK
|
||||||
|
private lateinit var converter: ExpressionToRplConverter
|
||||||
|
|
||||||
|
@MockK
|
||||||
|
private lateinit var unaryFunctions: Map<UnaryOperation, UnaryOperationFunction<out UnaryOperation>>
|
||||||
|
|
||||||
|
@MockK
|
||||||
|
private lateinit var binaryFunctions: Map<BinaryOperation, BinaryOperationFunction<out BinaryOperation>>
|
||||||
|
|
||||||
|
@InjectMockKs
|
||||||
|
private lateinit var calculator: CalculatorImpl
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testWithBinary() {
|
||||||
|
every { converter.convert(any()) } returns listOf(Operand("3"), Operand("2"), BinaryMinus)
|
||||||
|
every { binaryFunctions[BinaryMinus] } returns mockk {
|
||||||
|
every { apply(any(Operand::class), any(Operand::class)) } returns Operand("1")
|
||||||
|
}
|
||||||
|
val expression = Expression()
|
||||||
|
val result = assertDoesNotThrow { calculator.calculate(expression) }
|
||||||
|
|
||||||
|
verify { converter.convert(expression) }
|
||||||
|
verify { binaryFunctions[BinaryMinus] }
|
||||||
|
|
||||||
|
assertEquals(BigDecimal.ONE, result)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testWithUnary() {
|
||||||
|
every { converter.convert(any()) } returns listOf(Operand("2"), UnaryMinus)
|
||||||
|
every { unaryFunctions[UnaryMinus] } returns mockk {
|
||||||
|
every { apply(any(Operand::class)) } returns Operand("-2")
|
||||||
|
}
|
||||||
|
val expression = Expression()
|
||||||
|
val result = assertDoesNotThrow { calculator.calculate(expression) }
|
||||||
|
|
||||||
|
verify { converter.convert(expression) }
|
||||||
|
verify { unaryFunctions[UnaryMinus] }
|
||||||
|
|
||||||
|
assertEquals(BigDecimal.valueOf(-2), result)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
package ru.darktale.calc
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.assertDoesNotThrow
|
||||||
|
import org.junit.jupiter.api.extension.ExtensionContext
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest
|
||||||
|
import org.junit.jupiter.params.provider.Arguments
|
||||||
|
import org.junit.jupiter.params.provider.ArgumentsProvider
|
||||||
|
import org.junit.jupiter.params.provider.ArgumentsSource
|
||||||
|
import java.util.stream.Stream
|
||||||
|
import kotlin.test.assertContentEquals
|
||||||
|
|
||||||
|
class ExpressionToRplConverterImplTest {
|
||||||
|
private val converter = ExpressionToRplConverterImpl()
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ArgumentsSource(ExpressionProvider::class)
|
||||||
|
fun convert(expression: Expression, expectedTokens: List<CalculationToken>) {
|
||||||
|
val result = assertDoesNotThrow { converter.convert(expression) }
|
||||||
|
assertContentEquals(expectedTokens, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class ExpressionProvider : ArgumentsProvider {
|
||||||
|
override fun provideArguments(context: ExtensionContext): Stream<out Arguments> =
|
||||||
|
Stream.of(
|
||||||
|
Arguments.of(
|
||||||
|
Expression(Operand("3"), BinaryPlus, Operand("2")),
|
||||||
|
listOf(Operand("3"), Operand("2"), BinaryPlus)
|
||||||
|
),
|
||||||
|
Arguments.of(
|
||||||
|
Expression(Operand("3"), BinaryPlus, Operand("2"), BinaryPlus, Operand("1")),
|
||||||
|
listOf(Operand("3"), Operand("2"), BinaryPlus, Operand("1"), BinaryPlus)
|
||||||
|
),
|
||||||
|
Arguments.of(
|
||||||
|
Expression(Operand("3"), BinaryPlus, OpenBracket, Operand("2"), BinaryPlus, Operand("1"), CloseBracket),
|
||||||
|
listOf(Operand("3"), Operand("2"), Operand("1"), BinaryPlus, BinaryPlus)
|
||||||
|
),
|
||||||
|
Arguments.of(
|
||||||
|
Expression(Operand("3"), BinaryPlus, Operand("2"), Multiplication, Operand("1")),
|
||||||
|
listOf(Operand("3"), Operand("2"), Operand("1"), Multiplication, BinaryPlus)
|
||||||
|
),
|
||||||
|
Arguments.of(
|
||||||
|
Expression(Operand("3"), Multiplication, OpenBracket, Operand("2"), BinaryPlus, Operand("1"), CloseBracket),
|
||||||
|
listOf(Operand("3"), Operand("2"), Operand("1"), BinaryPlus, Multiplication)
|
||||||
|
),
|
||||||
|
Arguments.of(
|
||||||
|
Expression(UnaryMinus, Operand("3"), Multiplication, OpenBracket, UnaryMinus, Operand("2"), BinaryPlus, Operand("1"), CloseBracket),
|
||||||
|
listOf(Operand("3"), UnaryMinus, Operand("2"), UnaryMinus, Operand("1"), BinaryPlus, Multiplication)
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package ru.darktale.calc
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.DisplayName
|
||||||
|
import org.junit.jupiter.api.assertDoesNotThrow
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest
|
||||||
|
import org.junit.jupiter.params.provider.CsvSource
|
||||||
|
import kotlin.test.assertEquals
|
||||||
|
import kotlin.test.assertFailsWith
|
||||||
|
|
||||||
|
class NestedExpressionsParserTest {
|
||||||
|
private val parser = NestedExpressionsParser()
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@CsvSource(
|
||||||
|
"1+2",
|
||||||
|
".5", "0.5",
|
||||||
|
"1e+3", "1e-2", "1e0", ".4e1",
|
||||||
|
"(1+2)", "(1+2)/(3+4)", "((1+2)*(3+4))", "(0)",
|
||||||
|
"-1", "+0", "-.5",
|
||||||
|
"-1*(-1)", "-1*-1",
|
||||||
|
"sin45", "sin(45)","sin(45*2)",
|
||||||
|
)
|
||||||
|
@DisplayName(("Parse correct expressions"))
|
||||||
|
fun parseSuccess(expression: String) {
|
||||||
|
val result = assertDoesNotThrow { parser.parse(expression) }
|
||||||
|
assertEquals(expression, result.toString())
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@CsvSource(
|
||||||
|
"-", "+",
|
||||||
|
"(", ")", "(()", ")(", "()",
|
||||||
|
"1++", "++1", "+1+",
|
||||||
|
)
|
||||||
|
@DisplayName("Fail incorrect expressions")
|
||||||
|
fun parseException(expression: String) {
|
||||||
|
assertFailsWith<ParseException> { parser.parse(expression) }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package ru.darktale.calc
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest
|
||||||
|
import org.junit.jupiter.params.provider.CsvSource
|
||||||
|
/*
|
||||||
|
class ParserImplTest {
|
||||||
|
private val parser = ParserImpl()
|
||||||
|
|
||||||
|
@ParameterizedTest(name = "input {0} expects {1}")
|
||||||
|
@CsvSource(
|
||||||
|
"123; [123]",
|
||||||
|
"12+13; [12, +, 13]",
|
||||||
|
"120*9; [120, *, 9]",
|
||||||
|
"-11; [-, 11]",
|
||||||
|
"(3+2)*8; [(, 3, +, 2, ), *, 8]",
|
||||||
|
"1e3; [1e3]",
|
||||||
|
"1e+3; [1e+3]",
|
||||||
|
"1e-3; [1e-3]",
|
||||||
|
"1e0; [1e0]",
|
||||||
|
"1e0-3; [1e0, -, 3]",
|
||||||
|
delimiter = ';'
|
||||||
|
)
|
||||||
|
fun parseSimpleExpression(expression: String, expectedResult: String) {
|
||||||
|
val result = parser.parse(expression)
|
||||||
|
assertEquals(expression, result.toString())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
*/
|
||||||
Reference in New Issue
Block a user