Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Use string templates for toString.

Get rid of ;s
Fix "equals()" per Andrew Breslav's suggestion.
  • Loading branch information...
commit 3c08714cebdee8d242450920252aee7b1c1c3b56 1 parent 71c3a86
Ian Clarke authored
5 .gitignore
... ... @@ -1,7 +1,2 @@
1 1 target/
2 2 *.class
3   -
4   -# Package Files #
5   -*.jar
6   -*.war
7   -*.ear
34 .idea/workspace.xml
@@ -4,6 +4,7 @@
4 4 <list default="true" id="97cfbba1-dec8-4e92-ae1e-4d17cedb13f2" name="Default" comment="" />
5 5 <ignored path="RationalExp.iws" />
6 6 <ignored path=".idea/workspace.xml" />
  7 + <file path="/Rational.kt" changelist="97cfbba1-dec8-4e92-ae1e-4d17cedb13f2" time="1352125275011" ignored="false" />
7 8 <file path="/pom.xml" changelist="97cfbba1-dec8-4e92-ae1e-4d17cedb13f2" time="1352065857737" ignored="false" />
8 9 <file path="$PROJECT_DIR$/../amplify/src/main/java/amplify/scraper/Scraper.java" changelist="97cfbba1-dec8-4e92-ae1e-4d17cedb13f2" time="1351889886308" ignored="false" />
9 10 <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 @@
121 122 <file path="$PROJECT_DIR$/../bidder/src/main/java/com/onespot/bidder/utils/CreativeSyncService.java" changelist="97cfbba1-dec8-4e92-ae1e-4d17cedb13f2" time="1351789229386" ignored="false" />
122 123 <file path="$PROJECT_DIR$/../bidder/src/test/java/com/onespot/bidder/utils/CreativeSyncServiceTest.java" changelist="97cfbba1-dec8-4e92-ae1e-4d17cedb13f2" time="1351789229386" ignored="false" />
123 124 <file path="$PROJECT_DIR$/../amplify/src/main/java/amplify/adnetworks/appnexus/ANBudgetUpdater.java" changelist="97cfbba1-dec8-4e92-ae1e-4d17cedb13f2" time="1351789383896" ignored="false" />
124   - <file path="/Rational.kt" changelist="97cfbba1-dec8-4e92-ae1e-4d17cedb13f2" time="1352062979311" ignored="false" />
125 125 <option name="TRACKING_ENABLED" value="true" />
126 126 <option name="SHOW_DIALOG" value="false" />
127 127 <option name="HIGHLIGHT_CONFLICTS" value="true" />
@@ -182,19 +182,19 @@
182 182 </component>
183 183 <component name="FileEditorManager">
184 184 <leaf>
185   - <file leaf-file-name="pom.xml" pinned="false" current="true" current-in-tab="true">
  185 + <file leaf-file-name="pom.xml" pinned="false" current="false" current-in-tab="false">
186 186 <entry file="file://$PROJECT_DIR$/pom.xml">
187 187 <provider selected="true" editor-type-id="text-editor">
188   - <state line="9" column="24" selection-start="413" selection-end="413" vertical-scroll-proportion="0.074418604">
  188 + <state line="35" column="36" selection-start="1107" selection-end="1107" vertical-scroll-proportion="0.0">
189 189 <folding />
190 190 </state>
191 191 </provider>
192 192 </entry>
193 193 </file>
194   - <file leaf-file-name="Rational.kt" pinned="false" current="false" current-in-tab="false">
  194 + <file leaf-file-name="Rational.kt" pinned="false" current="true" current-in-tab="true">
195 195 <entry file="file://$PROJECT_DIR$/src/main/java/us/locut/Rational.kt">
196 196 <provider selected="true" editor-type-id="text-editor">
197   - <state line="91" column="5" selection-start="2862" selection-end="2862" vertical-scroll-proportion="0.0">
  197 + <state line="111" column="80" selection-start="3469" selection-end="3469" vertical-scroll-proportion="2.8784442">
198 198 <folding />
199 199 </state>
200 200 </provider>
@@ -211,8 +211,8 @@
211 211 <option name="changedFiles">
212 212 <list>
213 213 <option value="$PROJECT_DIR$/src/main/java/Rational.kt" />
214   - <option value="$PROJECT_DIR$/src/main/java/us/locut/Rational.kt" />
215 214 <option value="$PROJECT_DIR$/pom.xml" />
  215 + <option value="$PROJECT_DIR$/src/main/java/us/locut/Rational.kt" />
216 216 </list>
217 217 </option>
218 218 </component>
@@ -224,8 +224,8 @@
224 224 </option>
225 225 </component>
226 226 <component name="ProjectFrameBounds">
227   - <option name="x" value="349" />
228   - <option name="y" value="171" />
  227 + <option name="x" value="1086" />
  228 + <option name="y" value="98" />
229 229 <option name="width" value="1920" />
230 230 <option name="height" value="1121" />
231 231 </component>
@@ -288,7 +288,7 @@
288 288 <property name="options.splitter.main.proportions" value="0.3" />
289 289 <property name="recentsLimit" value="5" />
290 290 <property name="MemberChooser.sorted" value="false" />
291   - <property name="options.lastSelected" value="reference.settingsdialog.IDE.editor.colors.Font" />
  291 + <property name="options.lastSelected" value="project.propVCSSupport.Mappings" />
292 292 <property name="last_opened_file_path" value="$PROJECT_DIR$" />
293 293 <property name="MemberChooser.copyJavadoc" value="false" />
294 294 <property name="GoToClass.toSaveIncludeLibraries" value="false" />
@@ -360,7 +360,7 @@
360 360 <option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false" />
361 361 <option name="ALTERNATIVE_JRE_PATH" />
362 362 <option name="PASS_PARENT_ENVS" value="true" />
363   - <module name="RationalExp" />
  363 + <module name="RationalNumber" />
364 364 <envs />
365 365 <method />
366 366 </configuration>
@@ -453,14 +453,14 @@
453 453 <servers />
454 454 </component>
455 455 <component name="ToolWindowManager">
456   - <frame x="349" y="171" width="1920" height="1121" extended-state="0" />
  456 + <frame x="1086" y="98" width="1920" height="1121" extended-state="0" />
457 457 <editor active="true" />
458 458 <layout>
459 459 <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" />
460 460 <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" />
461 461 <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" />
462 462 <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" />
463   - <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" />
  463 + <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" />
464 464 <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" />
465 465 <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" />
466 466 <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 @@
469 469 <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" />
470 470 <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" />
471 471 <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" />
472   - <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" />
  472 + <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" />
473 473 <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" />
474 474 <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" />
475 475 <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 @@
611 611 </state>
612 612 </provider>
613 613 </entry>
614   - <entry file="file://$PROJECT_DIR$/src/main/java/us/locut/Rational.kt">
  614 + <entry file="file://$PROJECT_DIR$/pom.xml">
615 615 <provider selected="true" editor-type-id="text-editor">
616   - <state line="91" column="5" selection-start="2862" selection-end="2862" vertical-scroll-proportion="0.0">
  616 + <state line="35" column="36" selection-start="1107" selection-end="1107" vertical-scroll-proportion="0.0">
617 617 <folding />
618 618 </state>
619 619 </provider>
620 620 </entry>
621   - <entry file="file://$PROJECT_DIR$/pom.xml">
  621 + <entry file="file://$PROJECT_DIR$/src/main/java/us/locut/Rational.kt">
622 622 <provider selected="true" editor-type-id="text-editor">
623   - <state line="9" column="24" selection-start="413" selection-end="413" vertical-scroll-proportion="0.074418604">
  623 + <state line="111" column="80" selection-start="3469" selection-end="3469" vertical-scroll-proportion="2.8784442">
624 624 <folding />
625 625 </state>
626 626 </provider>
88 src/main/java/us/locut/Rational.kt
@@ -11,124 +11,122 @@ package us.locut
11 11 import com.google.common.base.Objects
12 12
13 13 fun main(args: Array<String>): Unit {
14   - println(Rational("20.4") + Rational("15.2"));
  14 + println(Rational("20.4") + Rational("15.2"))
15 15 }
16 16
17 17 fun Rational(val str: String): Rational {
18   - val dpIx = str.indexOf('.');
  18 + val dpIx = str.indexOf('.')
19 19 if (dpIx == -1) {
20   - return Rational(java.lang.Long.parseLong(str));
  20 + return Rational(java.lang.Long.parseLong(str))
21 21 }
22   - val raw = str.substring(0, dpIx) + str.substring(dpIx + 1, str.length);
  22 + val raw = str.substring(0, dpIx) + str.substring(dpIx + 1, str.length)
23 23
24   - return Rational.createSimplified(java.lang.Long.parseLong(raw), pow10((str.length - dpIx).toLong()));
  24 + return Rational.createSimplified(java.lang.Long.parseLong(raw), pow10((str.length - dpIx).toLong()))
25 25 }
26 26
27 27 fun Rational(val number : Number) : Rational {
28 28 return when (number) {
29   - is Int -> Rational.create(number.toLong(), 1);
30   - is Long -> Rational.create(number, 1);
31   - is Rational -> number;
  29 + is Int -> Rational.create(number.toLong(), 1)
  30 + is Long -> Rational.create(number, 1)
  31 + is Rational -> number
32 32 else -> {
33 33 // TODO: Make more efficient, should probably pull the double apart using Double.doubleToLongBits
34   - Rational(number.toDouble().toString());
  34 + Rational(number.toDouble().toString())
35 35 }
36 36 }
37 37 }
38 38
39 39 fun gcf(val a: Long, val b: Long): Long {
40   - return if (b == 0.toLong()) a else gcf(b, a % b);
  40 + return if (b == 0.toLong()) a else gcf(b, a % b)
41 41 }
42 42
43 43 private fun pow10(var exp: Long): Long {
44   - var result: Long = 1;
  44 + var result: Long = 1
45 45 while (exp > 0) {
46 46 if (exp and 1 > 0) {
47   - result *= 10;
  47 + result *= 10
48 48 }
49   - exp = exp shr 1;
  49 + exp = exp shr 1
50 50 }
51   - return result;
  51 + return result
52 52 }
53 53
54   -class Rational private(val nominator: Long, val denominator: Long, val simplified : Boolean): Number() {
  54 +class Rational private(val numerator: Long, val denominator: Long, val simplified : Boolean): Number() {
55 55 class object {
56 56 fun create(val nominator: Long, val denominator: Long) : Rational {
57   - return Rational(nominator, denominator, false);
  57 + return Rational(nominator, denominator, false)
58 58 }
59 59
60 60 fun createSimplified(val nominator: Long, val denominator: Long) : Rational {
61   - val gcd = gcf(nominator, denominator);
62   - return Rational(nominator / gcd, denominator / gcd, true);
  61 + val gcd = gcf(nominator, denominator)
  62 + return Rational(nominator / gcd, denominator / gcd, true)
63 63 }
64 64 }
65 65
66 66 public override fun hashCode(): Int {
67   - return Objects.hashCode(nominator, denominator);
  67 + return Objects.hashCode(numerator, denominator)
68 68 }
69 69 public override fun equals(other: Any?): Boolean {
70   - return when (other) {
71   - is Int -> denominator == 1.toLong() && nominator == other;
72   - is Long -> denominator == 1.toLong() && nominator == other;
73   - is Rational -> nominator * other.denominator == other.nominator * denominator;
74   - is Number -> Rational(other).equals(this);
75   - else -> false;
  70 + if (other is Rational) {
  71 + return numerator * other.denominator == other.numerator * denominator
  72 + } else {
  73 + return false
76 74 }
77 75 }
78 76 public override fun toFloat(): Float {
79   - return nominator.toFloat() / denominator.toFloat();
  77 + return numerator.toFloat() / denominator.toFloat()
80 78 }
81 79 public override fun toLong(): Long {
82   - return nominator / denominator;
  80 + return numerator / denominator
83 81 }
84 82 public override fun toInt(): Int {
85   - return (nominator / denominator) as Int;
  83 + return (numerator / denominator) as Int
86 84 }
87 85 public override fun toChar(): Char {
88   - return (nominator / denominator) as Char;
  86 + return (numerator / denominator) as Char
89 87 }
90 88 public override fun toShort(): Short {
91   - return (nominator / denominator) as Short;
  89 + return (numerator / denominator) as Short
92 90 }
93 91 public override fun toByte(): Byte {
94   - return (nominator / denominator) as Byte;
  92 + return (numerator / denominator) as Byte
95 93 }
96 94
97 95 public override fun toDouble(): Double {
98   - return nominator.toDouble() / denominator.toDouble();
  96 + return numerator.toDouble() / denominator.toDouble()
99 97 }
100 98
101 99 public fun minus(val other : Rational): Rational {
102   - val mn = nominator * other.denominator;
103   - val md = other.nominator * denominator;
104   - return Rational.createSimplified(mn - md, denominator * other.denominator);
  100 + val mn = numerator * other.denominator
  101 + val md = other.numerator * denominator
  102 + return Rational.createSimplified(mn - md, denominator * other.denominator)
105 103 }
106 104
107 105 public fun plus(val other : Rational): Rational {
108   - val mn = nominator * other.denominator;
109   - val md = other.nominator * denominator;
110   - return Rational.createSimplified(mn + md, denominator * other.denominator);
  106 + val mn = numerator * other.denominator
  107 + val md = other.numerator * denominator
  108 + return Rational.createSimplified(mn + md, denominator * other.denominator)
111 109 }
112 110
113 111 public fun times(val other: Long) : Rational {
114   - return Rational.createSimplified(nominator * other, denominator * other);
  112 + return Rational.createSimplified(numerator * other, denominator * other)
115 113 }
116 114
117 115 public fun times(val other: Rational): Rational {
118   - return Rational.createSimplified(nominator * other.nominator, denominator * other.denominator);
  116 + return Rational.createSimplified(numerator * other.numerator, denominator * other.denominator)
119 117 }
120 118
121 119 public fun div(val other: Rational): Rational {
122   - return Rational.createSimplified(nominator * other.denominator, denominator * other.nominator);
  120 + return Rational.createSimplified(numerator * other.denominator, denominator * other.numerator)
123 121 }
124 122
125 123 public fun simplify(): Rational {
126   - if (simplified) return this;
  124 + if (simplified) return this
127 125 else {
128   - val gcd = gcf(nominator, denominator);
129   - return Rational(nominator / gcd, denominator / gcd, true);
  126 + val gcd = gcf(numerator, denominator)
  127 + return Rational(numerator / gcd, denominator / gcd, true)
130 128 }
131 129 }
132 130
133   - public fun toString() : String = "("+nominator+"/"+denominator+")";
  131 + public fun toString() : String = "($numerator/$denominator)"
134 132 }

0 comments on commit 3c08714

Please sign in to comment.
Something went wrong with that request. Please try again.