Browse files

Use string templates for toString.

Get rid of ;s
Fix "equals()" per Andrew Breslav's suggestion.
  • Loading branch information...
1 parent 71c3a86 commit 3c08714cebdee8d242450920252aee7b1c1c3b56 @sanity committed Nov 5, 2012
Showing with 60 additions and 67 deletions.
  1. +0 −5 .gitignore
  2. +17 −17 .idea/workspace.xml
  3. +43 −45 src/main/java/us/locut/Rational.kt
View
5 .gitignore
@@ -1,7 +1,2 @@
target/
*.class
-
-# Package Files #
-*.jar
-*.war
-*.ear
View
34 .idea/workspace.xml
@@ -4,6 +4,7 @@
<list default="true" id="97cfbba1-dec8-4e92-ae1e-4d17cedb13f2" name="Default" comment="" />
<ignored path="RationalExp.iws" />
<ignored path=".idea/workspace.xml" />
+ <file path="/Rational.kt" changelist="97cfbba1-dec8-4e92-ae1e-4d17cedb13f2" time="1352125275011" ignored="false" />
<file path="/pom.xml" changelist="97cfbba1-dec8-4e92-ae1e-4d17cedb13f2" time="1352065857737" ignored="false" />
<file path="$PROJECT_DIR$/../amplify/src/main/java/amplify/scraper/Scraper.java" changelist="97cfbba1-dec8-4e92-ae1e-4d17cedb13f2" time="1351889886308" ignored="false" />
<file path="$PROJECT_DIR$/../bidder/src/main/java/com/onespot/bidder/appnexus/dataobjects/Bid.java" changelist="97cfbba1-dec8-4e92-ae1e-4d17cedb13f2" time="1351889886308" ignored="false" />
@@ -121,7 +122,6 @@
<file path="$PROJECT_DIR$/../bidder/src/main/java/com/onespot/bidder/utils/CreativeSyncService.java" changelist="97cfbba1-dec8-4e92-ae1e-4d17cedb13f2" time="1351789229386" ignored="false" />
<file path="$PROJECT_DIR$/../bidder/src/test/java/com/onespot/bidder/utils/CreativeSyncServiceTest.java" changelist="97cfbba1-dec8-4e92-ae1e-4d17cedb13f2" time="1351789229386" ignored="false" />
<file path="$PROJECT_DIR$/../amplify/src/main/java/amplify/adnetworks/appnexus/ANBudgetUpdater.java" changelist="97cfbba1-dec8-4e92-ae1e-4d17cedb13f2" time="1351789383896" ignored="false" />
- <file path="/Rational.kt" changelist="97cfbba1-dec8-4e92-ae1e-4d17cedb13f2" time="1352062979311" ignored="false" />
<option name="TRACKING_ENABLED" value="true" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@@ -182,19 +182,19 @@
</component>
<component name="FileEditorManager">
<leaf>
- <file leaf-file-name="pom.xml" pinned="false" current="true" current-in-tab="true">
+ <file leaf-file-name="pom.xml" pinned="false" current="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/pom.xml">
<provider selected="true" editor-type-id="text-editor">
- <state line="9" column="24" selection-start="413" selection-end="413" vertical-scroll-proportion="0.074418604">
+ <state line="35" column="36" selection-start="1107" selection-end="1107" vertical-scroll-proportion="0.0">
<folding />
</state>
</provider>
</entry>
</file>
- <file leaf-file-name="Rational.kt" pinned="false" current="false" current-in-tab="false">
+ <file leaf-file-name="Rational.kt" pinned="false" current="true" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/src/main/java/us/locut/Rational.kt">
<provider selected="true" editor-type-id="text-editor">
- <state line="91" column="5" selection-start="2862" selection-end="2862" vertical-scroll-proportion="0.0">
+ <state line="111" column="80" selection-start="3469" selection-end="3469" vertical-scroll-proportion="2.8784442">
<folding />
</state>
</provider>
@@ -211,8 +211,8 @@
<option name="changedFiles">
<list>
<option value="$PROJECT_DIR$/src/main/java/Rational.kt" />
- <option value="$PROJECT_DIR$/src/main/java/us/locut/Rational.kt" />
<option value="$PROJECT_DIR$/pom.xml" />
+ <option value="$PROJECT_DIR$/src/main/java/us/locut/Rational.kt" />
</list>
</option>
</component>
@@ -224,8 +224,8 @@
</option>
</component>
<component name="ProjectFrameBounds">
- <option name="x" value="349" />
- <option name="y" value="171" />
+ <option name="x" value="1086" />
+ <option name="y" value="98" />
<option name="width" value="1920" />
<option name="height" value="1121" />
</component>
@@ -288,7 +288,7 @@
<property name="options.splitter.main.proportions" value="0.3" />
<property name="recentsLimit" value="5" />
<property name="MemberChooser.sorted" value="false" />
- <property name="options.lastSelected" value="reference.settingsdialog.IDE.editor.colors.Font" />
+ <property name="options.lastSelected" value="project.propVCSSupport.Mappings" />
<property name="last_opened_file_path" value="$PROJECT_DIR$" />
<property name="MemberChooser.copyJavadoc" value="false" />
<property name="GoToClass.toSaveIncludeLibraries" value="false" />
@@ -360,7 +360,7 @@
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false" />
<option name="ALTERNATIVE_JRE_PATH" />
<option name="PASS_PARENT_ENVS" value="true" />
- <module name="RationalExp" />
+ <module name="RationalNumber" />
<envs />
<method />
</configuration>
@@ -453,14 +453,14 @@
<servers />
</component>
<component name="ToolWindowManager">
- <frame x="349" y="171" width="1920" height="1121" extended-state="0" />
+ <frame x="1086" y="98" width="1920" height="1121" extended-state="0" />
<editor active="true" />
<layout>
<window_info id="Changes" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="7" side_tool="false" content_ui="tabs" />
<window_info id="Palette" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="3" side_tool="false" content_ui="tabs" />
<window_info id="Database" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="3" side_tool="false" content_ui="tabs" />
<window_info id="Debug" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.4" sideWeight="0.5" order="3" side_tool="false" content_ui="tabs" />
- <window_info id="Event Log" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" weight="0.32901296" sideWeight="0.49839914" order="7" side_tool="true" content_ui="tabs" />
+ <window_info id="Event Log" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" weight="0.32967034" sideWeight="0.49839914" order="7" side_tool="true" content_ui="tabs" />
<window_info id="Favorites" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="2" side_tool="true" content_ui="tabs" />
<window_info id="Kotlin" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="3" side_tool="false" content_ui="tabs" />
<window_info id="Version Control" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="7" side_tool="false" content_ui="tabs" />
@@ -469,7 +469,7 @@
<window_info id="Maven Projects" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="3" side_tool="false" content_ui="tabs" />
<window_info id="Commander" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.4" sideWeight="0.5" order="0" side_tool="false" content_ui="tabs" />
<window_info id="Application Servers" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="7" side_tool="false" content_ui="tabs" />
- <window_info id="Project" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" weight="0.23692636" sideWeight="0.670987" order="0" side_tool="false" content_ui="combo" />
+ <window_info id="Project" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" weight="0.23692636" sideWeight="0.6703297" order="0" side_tool="false" content_ui="combo" />
<window_info id="Run" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.32967034" sideWeight="0.5" order="2" side_tool="false" content_ui="tabs" />
<window_info id="Cvs" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.25" sideWeight="0.5" order="4" side_tool="false" content_ui="tabs" />
<window_info id="Message" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="0" side_tool="false" content_ui="tabs" />
@@ -611,16 +611,16 @@
</state>
</provider>
</entry>
- <entry file="file://$PROJECT_DIR$/src/main/java/us/locut/Rational.kt">
+ <entry file="file://$PROJECT_DIR$/pom.xml">
<provider selected="true" editor-type-id="text-editor">
- <state line="91" column="5" selection-start="2862" selection-end="2862" vertical-scroll-proportion="0.0">
+ <state line="35" column="36" selection-start="1107" selection-end="1107" vertical-scroll-proportion="0.0">
<folding />
</state>
</provider>
</entry>
- <entry file="file://$PROJECT_DIR$/pom.xml">
+ <entry file="file://$PROJECT_DIR$/src/main/java/us/locut/Rational.kt">
<provider selected="true" editor-type-id="text-editor">
- <state line="9" column="24" selection-start="413" selection-end="413" vertical-scroll-proportion="0.074418604">
+ <state line="111" column="80" selection-start="3469" selection-end="3469" vertical-scroll-proportion="2.8784442">
<folding />
</state>
</provider>
View
88 src/main/java/us/locut/Rational.kt
@@ -11,124 +11,122 @@ package us.locut
import com.google.common.base.Objects
fun main(args: Array<String>): Unit {
- println(Rational("20.4") + Rational("15.2"));
+ println(Rational("20.4") + Rational("15.2"))
}
fun Rational(val str: String): Rational {
- val dpIx = str.indexOf('.');
+ val dpIx = str.indexOf('.')
if (dpIx == -1) {
- return Rational(java.lang.Long.parseLong(str));
+ return Rational(java.lang.Long.parseLong(str))
}
- val raw = str.substring(0, dpIx) + str.substring(dpIx + 1, str.length);
+ val raw = str.substring(0, dpIx) + str.substring(dpIx + 1, str.length)
- return Rational.createSimplified(java.lang.Long.parseLong(raw), pow10((str.length - dpIx).toLong()));
+ return Rational.createSimplified(java.lang.Long.parseLong(raw), pow10((str.length - dpIx).toLong()))
}
fun Rational(val number : Number) : Rational {
return when (number) {
- is Int -> Rational.create(number.toLong(), 1);
- is Long -> Rational.create(number, 1);
- is Rational -> number;
+ is Int -> Rational.create(number.toLong(), 1)
+ is Long -> Rational.create(number, 1)
+ is Rational -> number
else -> {
// TODO: Make more efficient, should probably pull the double apart using Double.doubleToLongBits
- Rational(number.toDouble().toString());
+ Rational(number.toDouble().toString())
}
}
}
fun gcf(val a: Long, val b: Long): Long {
- return if (b == 0.toLong()) a else gcf(b, a % b);
+ return if (b == 0.toLong()) a else gcf(b, a % b)
}
private fun pow10(var exp: Long): Long {
- var result: Long = 1;
+ var result: Long = 1
while (exp > 0) {
if (exp and 1 > 0) {
- result *= 10;
+ result *= 10
}
- exp = exp shr 1;
+ exp = exp shr 1
}
- return result;
+ return result
}
-class Rational private(val nominator: Long, val denominator: Long, val simplified : Boolean): Number() {
+class Rational private(val numerator: Long, val denominator: Long, val simplified : Boolean): Number() {
class object {
fun create(val nominator: Long, val denominator: Long) : Rational {
- return Rational(nominator, denominator, false);
+ return Rational(nominator, denominator, false)
}
fun createSimplified(val nominator: Long, val denominator: Long) : Rational {
- val gcd = gcf(nominator, denominator);
- return Rational(nominator / gcd, denominator / gcd, true);
+ val gcd = gcf(nominator, denominator)
+ return Rational(nominator / gcd, denominator / gcd, true)
}
}
public override fun hashCode(): Int {
- return Objects.hashCode(nominator, denominator);
+ return Objects.hashCode(numerator, denominator)
}
public override fun equals(other: Any?): Boolean {
- return when (other) {
- is Int -> denominator == 1.toLong() && nominator == other;
- is Long -> denominator == 1.toLong() && nominator == other;
- is Rational -> nominator * other.denominator == other.nominator * denominator;
- is Number -> Rational(other).equals(this);
- else -> false;
+ if (other is Rational) {
+ return numerator * other.denominator == other.numerator * denominator
+ } else {
+ return false
}
}
public override fun toFloat(): Float {
- return nominator.toFloat() / denominator.toFloat();
+ return numerator.toFloat() / denominator.toFloat()
}
public override fun toLong(): Long {
- return nominator / denominator;
+ return numerator / denominator
}
public override fun toInt(): Int {
- return (nominator / denominator) as Int;
+ return (numerator / denominator) as Int
}
public override fun toChar(): Char {
- return (nominator / denominator) as Char;
+ return (numerator / denominator) as Char
}
public override fun toShort(): Short {
- return (nominator / denominator) as Short;
+ return (numerator / denominator) as Short
}
public override fun toByte(): Byte {
- return (nominator / denominator) as Byte;
+ return (numerator / denominator) as Byte
}
public override fun toDouble(): Double {
- return nominator.toDouble() / denominator.toDouble();
+ return numerator.toDouble() / denominator.toDouble()
}
public fun minus(val other : Rational): Rational {
- val mn = nominator * other.denominator;
- val md = other.nominator * denominator;
- return Rational.createSimplified(mn - md, denominator * other.denominator);
+ val mn = numerator * other.denominator
+ val md = other.numerator * denominator
+ return Rational.createSimplified(mn - md, denominator * other.denominator)
}
public fun plus(val other : Rational): Rational {
- val mn = nominator * other.denominator;
- val md = other.nominator * denominator;
- return Rational.createSimplified(mn + md, denominator * other.denominator);
+ val mn = numerator * other.denominator
+ val md = other.numerator * denominator
+ return Rational.createSimplified(mn + md, denominator * other.denominator)
}
public fun times(val other: Long) : Rational {
- return Rational.createSimplified(nominator * other, denominator * other);
+ return Rational.createSimplified(numerator * other, denominator * other)
}
public fun times(val other: Rational): Rational {
- return Rational.createSimplified(nominator * other.nominator, denominator * other.denominator);
+ return Rational.createSimplified(numerator * other.numerator, denominator * other.denominator)
}
public fun div(val other: Rational): Rational {
- return Rational.createSimplified(nominator * other.denominator, denominator * other.nominator);
+ return Rational.createSimplified(numerator * other.denominator, denominator * other.numerator)
}
public fun simplify(): Rational {
- if (simplified) return this;
+ if (simplified) return this
else {
- val gcd = gcf(nominator, denominator);
- return Rational(nominator / gcd, denominator / gcd, true);
+ val gcd = gcf(numerator, denominator)
+ return Rational(numerator / gcd, denominator / gcd, true)
}
}
- public fun toString() : String = "("+nominator+"/"+denominator+")";
+ public fun toString() : String = "($numerator/$denominator)"
}

0 comments on commit 3c08714

Please sign in to comment.