From 37a9073eab6461b26c45261f7613f395c6cfc888 Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Fri, 10 Jan 2025 16:52:33 +0300 Subject: [PATCH 1/2] bug(#3790): less --- .../src/main/java/org/eolang/Action.java | 39 ----------- .../src/main/java/org/eolang/AtComposite.java | 2 +- .../src/main/java/org/eolang/AtLogged.java | 5 +- .../src/main/java/org/eolang/AtOnce.java | 3 +- .../src/main/java/org/eolang/AtRho.java | 7 +- .../src/main/java/org/eolang/AtSetRho.java | 57 ---------------- .../src/main/java/org/eolang/AtVoid.java | 4 +- .../eolang/{AtGetOnly.java => AtWithRho.java} | 41 ++++++----- .../src/main/java/org/eolang/AtomSafe.java | 6 +- eo-runtime/src/main/java/org/eolang/Attr.java | 5 +- eo-runtime/src/main/java/org/eolang/Data.java | 13 ++-- .../src/main/java/org/eolang/PhDefault.java | 63 ++++++++--------- .../src/main/java/org/eolang/PhFake.java | 54 --------------- .../src/main/java/org/eolang/PhLambda.java | 40 ----------- .../src/main/java/org/eolang/PhLogged.java | 18 +++-- .../src/main/java/org/eolang/PhOnce.java | 13 ++-- .../src/main/java/org/eolang/PhPackage.java | 9 ++- .../src/main/java/org/eolang/PhSafe.java | 36 +++++++--- eo-runtime/src/main/java/org/eolang/Phi.java | 21 ++++-- .../src/main/java/org/eolang/XmirPackage.java | 48 ------------- .../EOorg/EOeolang/EOio/InputOutputTest.java | 10 +-- .../test/java/EOorg/EOeolang/HeapsTest.java | 68 +++++++++++++------ .../java/org/eolang/AtEnvelope.java | 9 +-- .../java/org/eolang/AtSimple.java | 11 ++- .../test/java/org/eolang/PhDefaultTest.java | 2 +- 25 files changed, 213 insertions(+), 371 deletions(-) delete mode 100644 eo-runtime/src/main/java/org/eolang/Action.java delete mode 100644 eo-runtime/src/main/java/org/eolang/AtSetRho.java rename eo-runtime/src/main/java/org/eolang/{AtGetOnly.java => AtWithRho.java} (66%) delete mode 100644 eo-runtime/src/main/java/org/eolang/PhFake.java delete mode 100644 eo-runtime/src/main/java/org/eolang/PhLambda.java delete mode 100644 eo-runtime/src/main/java/org/eolang/XmirPackage.java rename eo-runtime/src/{main => test}/java/org/eolang/AtEnvelope.java (87%) rename eo-runtime/src/{main => test}/java/org/eolang/AtSimple.java (86%) diff --git a/eo-runtime/src/main/java/org/eolang/Action.java b/eo-runtime/src/main/java/org/eolang/Action.java deleted file mode 100644 index 518fefc39f..0000000000 --- a/eo-runtime/src/main/java/org/eolang/Action.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2025 Objectionary.com - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package org.eolang; - -/** - * The action. - * @param The type - * @since 0.41.0 - */ -public interface Action { - - /** - * Run it. - * @return The value - */ - T act(); -} diff --git a/eo-runtime/src/main/java/org/eolang/AtComposite.java b/eo-runtime/src/main/java/org/eolang/AtComposite.java index 9ef64951af..12f1d4237e 100644 --- a/eo-runtime/src/main/java/org/eolang/AtComposite.java +++ b/eo-runtime/src/main/java/org/eolang/AtComposite.java @@ -65,7 +65,7 @@ public Phi get() { } @Override - public boolean put(final Phi phi) { + public void put(final Phi phi) { throw new ExReadOnly( "Can't overwrite lambda expression" ); diff --git a/eo-runtime/src/main/java/org/eolang/AtLogged.java b/eo-runtime/src/main/java/org/eolang/AtLogged.java index 4d0773347c..5e878b3a91 100644 --- a/eo-runtime/src/main/java/org/eolang/AtLogged.java +++ b/eo-runtime/src/main/java/org/eolang/AtLogged.java @@ -87,10 +87,9 @@ public Phi get() { } @Override - public boolean put(final Phi src) { + public void put(final Phi src) { this.log.info(String.format(" %s.put()...\n", this.owner)); - final boolean ret = this.origin.put(src); + this.origin.put(src); this.log.info(String.format(" %s.put()!\n", this.owner)); - return ret; } } diff --git a/eo-runtime/src/main/java/org/eolang/AtOnce.java b/eo-runtime/src/main/java/org/eolang/AtOnce.java index fdea6373dd..bc19bdc183 100644 --- a/eo-runtime/src/main/java/org/eolang/AtOnce.java +++ b/eo-runtime/src/main/java/org/eolang/AtOnce.java @@ -70,7 +70,7 @@ public Phi get() { } @Override - public boolean put(final Phi phi) { + public void put(final Phi phi) { throw new ExReadOnly( String.format( "Can't overwrite the \"%s\" attribute", @@ -78,5 +78,4 @@ public boolean put(final Phi phi) { ) ); } - } diff --git a/eo-runtime/src/main/java/org/eolang/AtRho.java b/eo-runtime/src/main/java/org/eolang/AtRho.java index 8a7002a0ab..89a99de2ae 100644 --- a/eo-runtime/src/main/java/org/eolang/AtRho.java +++ b/eo-runtime/src/main/java/org/eolang/AtRho.java @@ -70,14 +70,9 @@ public Phi get() { } @Override - public boolean put(final Phi phi) { - final boolean ret; + public void put(final Phi phi) { if (this.rho.get() == null) { this.rho.set(phi); - ret = true; - } else { - ret = false; } - return ret; } } diff --git a/eo-runtime/src/main/java/org/eolang/AtSetRho.java b/eo-runtime/src/main/java/org/eolang/AtSetRho.java deleted file mode 100644 index 6ff241e11e..0000000000 --- a/eo-runtime/src/main/java/org/eolang/AtSetRho.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2025 Objectionary.com - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package org.eolang; - -/** - * The attribute tries to copy object and set \rho to it. - * - *

If the name of the attribute is {@link Attr#RHO} - just object is returned.

- * - * @since 0.36.0 - */ -final class AtSetRho extends AtEnvelope { - /** - * Ctor. - * @param attr Origin attribute - * @param rho Rho that will be set - * @param name Name of the attribute - */ - AtSetRho(final Attr attr, final Phi rho, final String name) { - super( - new AtGetOnly( - () -> { - Phi ret = attr.get(); - if (!name.equals(Attr.RHO)) { - final Phi copy = ret.copy(); - if (copy.put(Attr.RHO, rho)) { - ret = copy; - } - } - return ret; - } - ) - ); - } -} diff --git a/eo-runtime/src/main/java/org/eolang/AtVoid.java b/eo-runtime/src/main/java/org/eolang/AtVoid.java index b6b99d98f1..2a1ec38c25 100644 --- a/eo-runtime/src/main/java/org/eolang/AtVoid.java +++ b/eo-runtime/src/main/java/org/eolang/AtVoid.java @@ -89,7 +89,7 @@ public Phi get() { } @Override - public boolean put(final Phi phi) { + public void put(final Phi phi) { if (this.object.get() == null) { this.object.set(phi); } else { @@ -100,7 +100,5 @@ public boolean put(final Phi phi) { ) ); } - return true; } - } diff --git a/eo-runtime/src/main/java/org/eolang/AtGetOnly.java b/eo-runtime/src/main/java/org/eolang/AtWithRho.java similarity index 66% rename from eo-runtime/src/main/java/org/eolang/AtGetOnly.java rename to eo-runtime/src/main/java/org/eolang/AtWithRho.java index deb37655fc..bb25fffa51 100644 --- a/eo-runtime/src/main/java/org/eolang/AtGetOnly.java +++ b/eo-runtime/src/main/java/org/eolang/AtWithRho.java @@ -24,44 +24,51 @@ package org.eolang; -import java.util.function.Supplier; - /** - * Attribute that only gets objects. - * + * The attribute that tries to copy object and set \rho to it if it has not already set. * @since 0.36.0 */ -final class AtGetOnly implements Attr { +final class AtWithRho implements Attr { + /** + * Original attribute. + */ + private final Attr origin; /** - * Get object supplier. + * Rho. */ - private final Supplier function; + private final Phi rho; /** * Ctor. - * @param func Get object function + * @param attr Attribute + * @param rho Rho */ - AtGetOnly(final Supplier func) { - this.function = func; + AtWithRho(final Attr attr, final Phi rho) { + this.origin = attr; + this.rho = rho; } @Override public Attr copy(final Phi self) { - throw new UnsupportedOperationException( - "Should never happen" + return new AtWithRho( + this.origin.copy(self), + self ); } @Override public Phi get() { - return this.function.get(); + Phi ret = this.origin.get(); + if (!ret.hasRho()) { + ret = ret.copy(); + ret.put(Attr.RHO, this.rho); + } + return ret; } @Override - public boolean put(final Phi phi) { - throw new UnsupportedOperationException( - "Should never happen" - ); + public void put(final Phi phi) { + this.origin.put(phi); } } diff --git a/eo-runtime/src/main/java/org/eolang/AtomSafe.java b/eo-runtime/src/main/java/org/eolang/AtomSafe.java index 94c1137d93..799a17cbe9 100644 --- a/eo-runtime/src/main/java/org/eolang/AtomSafe.java +++ b/eo-runtime/src/main/java/org/eolang/AtomSafe.java @@ -37,10 +37,10 @@ public final class AtomSafe implements Atom { /** * Ctor. - * @param atom Original atom. + * @param atom Phi as atom. */ - public AtomSafe(final Atom atom) { - this.origin = atom; + public AtomSafe(final Phi atom) { + this.origin = (Atom) atom; } @Override diff --git a/eo-runtime/src/main/java/org/eolang/Attr.java b/eo-runtime/src/main/java/org/eolang/Attr.java index 7c4c6f5ad6..c1958c4074 100644 --- a/eo-runtime/src/main/java/org/eolang/Attr.java +++ b/eo-runtime/src/main/java/org/eolang/Attr.java @@ -56,6 +56,8 @@ public interface Attr { /** * Take the object out. * + *

If attribute is not set - throws {@link ExUnset}.

+ * * @return The object */ Phi get(); @@ -64,7 +66,6 @@ public interface Attr { * Put a new object in. * * @param phi The object to put - * @return Was attribute set */ - boolean put(Phi phi); + void put(Phi phi); } diff --git a/eo-runtime/src/main/java/org/eolang/Data.java b/eo-runtime/src/main/java/org/eolang/Data.java index 5edd3b36e7..cbae982e20 100644 --- a/eo-runtime/src/main/java/org/eolang/Data.java +++ b/eo-runtime/src/main/java/org/eolang/Data.java @@ -85,19 +85,24 @@ public Phi copy() { return this.object.copy(); } + @Override + public boolean hasRho() { + return this.object.hasRho(); + } + @Override public Phi take(final String name) { return this.object.take(name); } @Override - public boolean put(final int pos, final Phi obj) { - return this.object.put(pos, obj); + public void put(final int pos, final Phi obj) { + this.object.put(pos, obj); } @Override - public boolean put(final String name, final Phi obj) { - return this.object.put(name, obj); + public void put(final String name, final Phi obj) { + this.object.put(name, obj); } @Override diff --git a/eo-runtime/src/main/java/org/eolang/PhDefault.java b/eo-runtime/src/main/java/org/eolang/PhDefault.java index f731b8d65a..d8ae1d4be9 100644 --- a/eo-runtime/src/main/java/org/eolang/PhDefault.java +++ b/eo-runtime/src/main/java/org/eolang/PhDefault.java @@ -96,12 +96,10 @@ public PhDefault() { * Ctor. * @param dta Object data */ - @SuppressWarnings("PMD.ConstructorOnlyInitializesOrCallOtherConstructors") public PhDefault(final byte[] dta) { this.data = Optional.ofNullable(dta); - this.attrs = new HashMap<>(0); + this.attrs = Map.of(Attr.RHO, new AtRho()); this.order = new HashMap<>(0); - this.add(Attr.RHO, new AtRho()); } @Override @@ -130,12 +128,23 @@ public final Phi copy() { } @Override - public boolean put(final int pos, final Phi object) { - return this.put(this.attr(pos), object); + public boolean hasRho() { + boolean has = true; + try { + this.attrs.get(Attr.RHO).get(); + } catch (final ExUnset exception) { + has = false; + } + return has; + } + + @Override + public void put(final int pos, final Phi object) { + this.put(this.attr(pos), object); } @Override - public boolean put(final String name, final Phi object) { + public void put(final String name, final Phi object) { if (!this.attrs.containsKey(name)) { throw new ExUnset( String.format( @@ -144,7 +153,7 @@ public boolean put(final String name, final Phi object) { ) ); } - return this.attrs.get(name).put(object); + this.attrs.get(name).put(object); } @Override @@ -152,37 +161,25 @@ public Phi take(final String name) { PhDefault.NESTING.set(PhDefault.NESTING.get() + 1); final Phi object; if (this.attrs.containsKey(name)) { - object = new AtSetRho( - this.attrs.get(name), - this, - name - ).get(); + object = this.attrs.get(name).get(); } else if (name.equals(Attr.LAMBDA)) { - object = new AtSetRho( - new AtSimple(new AtomSafe((Atom) this).lambda()), - this, - name - ).get(); + object = new AtomSafe(this).lambda(); } else if (this instanceof Atom) { object = this.take(Attr.LAMBDA).take(name); } else if (this.attrs.containsKey(Attr.PHI)) { object = this.take(Attr.PHI).take(name); } else { - object = new AtGetOnly( - () -> { - throw new ExUnset( - String.format( - "Can't #take(\"%s\"), the attribute is absent among other %d attrs of %s:(%s), %s and %s are also absent", - name, - this.attrs.size(), - this.forma(), - String.join(", ", this.attrs.keySet()), - Attr.PHI, - Attr.LAMBDA - ) - ); - } - ).get(); + throw new ExUnset( + String.format( + "Can't #take(\"%s\"), the attribute is absent among other %d attrs of %s:(%s), %s and %s are also absent", + name, + this.attrs.size(), + this.forma(), + String.join(", ", this.attrs.keySet()), + Attr.PHI, + Attr.LAMBDA + ) + ); } PhDefault.debug( String.format( @@ -251,7 +248,7 @@ protected final void add(final String name, final Attr attr) { if (PhDefault.SORTABLE.matcher(name).matches()) { this.order.put(this.order.size(), name); } - this.attrs.put(name, attr); + this.attrs.put(name, new AtWithRho(attr, this)); } /** diff --git a/eo-runtime/src/main/java/org/eolang/PhFake.java b/eo-runtime/src/main/java/org/eolang/PhFake.java deleted file mode 100644 index 7459c63a89..0000000000 --- a/eo-runtime/src/main/java/org/eolang/PhFake.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2025 Objectionary.com - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -/* - * @checkstyle PackageNameCheck (4 lines) - */ -package org.eolang; - -import java.util.function.Supplier; - -/** - * Fake object, mostly for unit tests. - * - * @since 0.29 - */ -public final class PhFake extends PhDefault { - /** - * Ctor. - */ - public PhFake() { - this(() -> Phi.Φ); - } - - /** - * Ctor. - * @param sup The function to return the real object - */ - @SuppressWarnings("PMD.ConstructorOnlyInitializesOrCallOtherConstructors") - public PhFake(final Supplier sup) { - this.add("args", new AtVoid("args")); - this.add("φ", new AtComposite(this, rho -> sup.get())); - } -} diff --git a/eo-runtime/src/main/java/org/eolang/PhLambda.java b/eo-runtime/src/main/java/org/eolang/PhLambda.java deleted file mode 100644 index 70e2f64265..0000000000 --- a/eo-runtime/src/main/java/org/eolang/PhLambda.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2025 Objectionary.com - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package org.eolang; - -/** - * A method-calling object. - * - * @since 0.1 - */ -public final class PhLambda extends PhOnce { - PhLambda() { - super( - () -> { - return new PhDefault(); - } - ); - } -} diff --git a/eo-runtime/src/main/java/org/eolang/PhLogged.java b/eo-runtime/src/main/java/org/eolang/PhLogged.java index f678f2a582..4bd4c2dfd9 100644 --- a/eo-runtime/src/main/java/org/eolang/PhLogged.java +++ b/eo-runtime/src/main/java/org/eolang/PhLogged.java @@ -55,6 +55,14 @@ public Phi copy() { return ret; } + @Override + public boolean hasRho() { + System.out.printf("%d.hasRho()...\n", this.hashCode()); + final boolean ret = this.origin.hasRho(); + System.out.printf("%d.hasRho()! -> %b\n", this.hashCode(), ret); + return ret; + } + @Override public Phi take(final String name) { System.out.printf("%d.take(\"%s\")...\n", this.hashCode(), name); @@ -64,19 +72,17 @@ public Phi take(final String name) { } @Override - public boolean put(final int pos, final Phi object) { + public void put(final int pos, final Phi object) { System.out.printf("%d.put(%d, %d)...\n", this.hashCode(), pos, object.hashCode()); - final boolean ret = this.origin.put(pos, object); + this.origin.put(pos, object); System.out.printf("%d.put(%d, %d)!\n", this.hashCode(), pos, object.hashCode()); - return ret; } @Override - public boolean put(final String name, final Phi object) { + public void put(final String name, final Phi object) { System.out.printf("%d.put(\"%s\", %d)...\n", this.hashCode(), name, object.hashCode()); - final boolean ret = this.origin.put(name, object); + this.origin.put(name, object); System.out.printf("%d.put(\"%s\", %d)!\n", this.hashCode(), name, object.hashCode()); - return ret; } @Override diff --git a/eo-runtime/src/main/java/org/eolang/PhOnce.java b/eo-runtime/src/main/java/org/eolang/PhOnce.java index ca1de124f4..82d0b43756 100644 --- a/eo-runtime/src/main/java/org/eolang/PhOnce.java +++ b/eo-runtime/src/main/java/org/eolang/PhOnce.java @@ -80,19 +80,24 @@ public Phi copy() { ); } + @Override + public boolean hasRho() { + return this.object.get().hasRho(); + } + @Override public Phi take(final String name) { return this.object.get().take(name); } @Override - public boolean put(final int pos, final Phi obj) { - return this.object.get().put(pos, obj); + public void put(final int pos, final Phi obj) { + this.object.get().put(pos, obj); } @Override - public boolean put(final String name, final Phi obj) { - return this.object.get().put(name, obj); + public void put(final String name, final Phi obj) { + this.object.get().put(name, obj); } @Override diff --git a/eo-runtime/src/main/java/org/eolang/PhPackage.java b/eo-runtime/src/main/java/org/eolang/PhPackage.java index 207cbd75e2..e3112d06f8 100644 --- a/eo-runtime/src/main/java/org/eolang/PhPackage.java +++ b/eo-runtime/src/main/java/org/eolang/PhPackage.java @@ -77,6 +77,11 @@ public Phi copy() { ); } + @Override + public boolean hasRho() { + return true; + } + @Override public Phi take(final String name) { final String obj = this.eoPackage(name); @@ -94,7 +99,7 @@ public Phi take(final String name) { } @Override - public boolean put(final int pos, final Phi object) { + public void put(final int pos, final Phi object) { throw new IllegalStateException( String.format( "Can't #put(%d, %s) to package object \"%s\"", @@ -104,7 +109,7 @@ public boolean put(final int pos, final Phi object) { } @Override - public boolean put(final String name, final Phi object) { + public void put(final String name, final Phi object) { throw new IllegalStateException( String.format( "Can't #put(%s, %s) to package object \"%s\"", diff --git a/eo-runtime/src/main/java/org/eolang/PhSafe.java b/eo-runtime/src/main/java/org/eolang/PhSafe.java index 87adb0fe24..d6d1bba5af 100644 --- a/eo-runtime/src/main/java/org/eolang/PhSafe.java +++ b/eo-runtime/src/main/java/org/eolang/PhSafe.java @@ -27,6 +27,7 @@ import EOorg.EOeolang.EOerror; import java.util.LinkedList; import java.util.List; +import java.util.function.Supplier; /** * An object with coordinates (line and position) and a safe @@ -129,19 +130,24 @@ public Phi copy() { ); } + @Override + public boolean hasRho() { + return this.through(this.origin::hasRho); + } + @Override public Phi take(final String name) { return this.through(() -> this.origin.take(name)); } @Override - public boolean put(final int pos, final Phi object) { - return this.through(() -> this.origin.put(pos, object)); + public void put(final int pos, final Phi object) { + this.through(() -> this.origin.put(pos, object)); } @Override - public boolean put(final String nme, final Phi object) { - return this.through(() -> this.origin.put(nme, object)); + public void put(final String nme, final Phi object) { + this.through(() -> this.origin.put(nme, object)); } @Override @@ -161,7 +167,21 @@ public byte[] delta() { @Override public Phi lambda() { - return this.through(() -> new AtomSafe((Atom) this.origin).lambda(), ".λ"); + return this.through(new AtomSafe(this.origin)::lambda, ".λ"); + } + + /** + * Helper, for other methods. + * @param action The action + */ + private void through(final Runnable action) { + this.through( + () -> { + action.run(); + return true; + }, + "" + ); } /** @@ -170,7 +190,7 @@ public Phi lambda() { * @param Type of result * @return Result */ - private T through(final Action action) { + private T through(final Supplier action) { return this.through(action, ""); } @@ -188,9 +208,9 @@ private T through(final Action action) { * @checkstyle IllegalCatchCheck (20 lines) */ @SuppressWarnings({"PMD.AvoidCatchingThrowable", "PMD.PreserveStackTrace"}) - private T through(final Action action, final String suffix) { + private T through(final Supplier action, final String suffix) { try { - return action.act(); + return action.get(); } catch (final EOerror.ExError ex) { throw new EOerror.ExError(ex, this.label(suffix)); } catch (final ExAbstract ex) { diff --git a/eo-runtime/src/main/java/org/eolang/Phi.java b/eo-runtime/src/main/java/org/eolang/Phi.java index cbd5084999..d15dd228cf 100644 --- a/eo-runtime/src/main/java/org/eolang/Phi.java +++ b/eo-runtime/src/main/java/org/eolang/Phi.java @@ -66,20 +66,25 @@ public Phi copy() { return Phi.Φ; } + @Override + public boolean hasRho() { + return false; + } + @Override public Phi take(final String name) { return this.pkg.take(name); } @Override - public boolean put(final int pos, final Phi object) { + public void put(final int pos, final Phi object) { throw new IllegalStateException( String.format("Can't #put(%d, %s) to Φ", pos, object) ); } @Override - public boolean put(final String name, final Phi object) { + public void put(final String name, final Phi object) { throw new IllegalStateException( String.format("Can't #put(%s, %s) to Φ", name, object) ); @@ -110,6 +115,12 @@ public byte[] delta() { */ Phi copy(); + /** + * Returns true if object has bound rho attribute. + * @return True if object has rho bound attribute + */ + boolean hasRho(); + /** * Take object by name of the attribute. * @param name The name of the attribute @@ -121,17 +132,15 @@ public byte[] delta() { * Put object by position of the attribute. * @param pos The position of the attribute. * @param object The object to put - * @return Was attribute set */ - boolean put(int pos, Phi object); + void put(int pos, Phi object); /** * Put object by name of the attribute. * @param name The name of the attribute. * @param object The object to put - * @return Was attribute set */ - boolean put(String name, Phi object); + void put(String name, Phi object); /** * Get code locator of the phi. diff --git a/eo-runtime/src/main/java/org/eolang/XmirPackage.java b/eo-runtime/src/main/java/org/eolang/XmirPackage.java deleted file mode 100644 index f7dd91f60d..0000000000 --- a/eo-runtime/src/main/java/org/eolang/XmirPackage.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2025 Objectionary.com - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package org.eolang; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * Annotation for a Java package made from XMIR meta. - * - * @since 0.49 - */ -@Target(ElementType.PACKAGE) -@Retention(RetentionPolicy.CLASS) -public @interface XmirPackage { - - /** - * The name of the package in EO. - * - * @return The name as it is in EO - */ - String value() default ""; - -} diff --git a/eo-runtime/src/test/java/EOorg/EOeolang/EOio/InputOutputTest.java b/eo-runtime/src/test/java/EOorg/EOeolang/EOio/InputOutputTest.java index 8d17de84b0..eee591868b 100644 --- a/eo-runtime/src/test/java/EOorg/EOeolang/EOio/InputOutputTest.java +++ b/eo-runtime/src/test/java/EOorg/EOeolang/EOio/InputOutputTest.java @@ -323,16 +323,15 @@ void writesToConsoleSequentially(@Mktmp final Path temp) throws IOException { final File file = InputOutputTest.redirectedStdout( temp, () -> { - final Phi buffer = new Data.ToPhi("Ha"); final Phi first = new PhWith( Phi.Φ.take(InputOutputTest.CONSOLE).take(InputOutputTest.WRITE).copy(), - 0, buffer + 0, new Data.ToPhi("Hey") ); final Phi second = new PhWith( new PhCopy( new PhMethod(first, InputOutputTest.WRITE) ), - 0, buffer + 0, new Data.ToPhi("There") ); new Dataized(second).take(); } @@ -340,7 +339,10 @@ void writesToConsoleSequentially(@Mktmp final Path temp) throws IOException { MatcherAssert.assertThat( "The 'console.write' should have return output block ready to write again, but it didn't", Files.readString(Paths.get(file.getAbsolutePath()), StandardCharsets.UTF_8), - Matchers.containsString("HaHa") + Matchers.allOf( + Matchers.containsString("Hey"), + Matchers.containsString("There") + ) ); } diff --git a/eo-runtime/src/test/java/EOorg/EOeolang/HeapsTest.java b/eo-runtime/src/test/java/EOorg/EOeolang/HeapsTest.java index 84480e28c4..0fb0e63921 100644 --- a/eo-runtime/src/test/java/EOorg/EOeolang/HeapsTest.java +++ b/eo-runtime/src/test/java/EOorg/EOeolang/HeapsTest.java @@ -28,9 +28,12 @@ */ package EOorg.EOeolang; // NOPMD +import java.util.function.Supplier; +import org.eolang.AtComposite; import org.eolang.AtCompositeTest; +import org.eolang.AtVoid; import org.eolang.ExFailure; -import org.eolang.PhFake; +import org.eolang.PhDefault; import org.eolang.Phi; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; @@ -51,7 +54,7 @@ final class HeapsTest { @Test void allocatesMemory() { - final int idx = HeapsTest.HEAPS.malloc(new PhFake(), 10); + final int idx = HeapsTest.HEAPS.malloc(new HeapsTest.PhFake(), 10); Assertions.assertDoesNotThrow( () -> HeapsTest.HEAPS.read(idx, 0, 10), AtCompositeTest.TO_ADD_MESSAGE @@ -61,7 +64,7 @@ void allocatesMemory() { @Test void failsOnDoubleAllocation() { - final Phi phi = new PhFake(); + final Phi phi = new HeapsTest.PhFake(); final int idx = HeapsTest.HEAPS.malloc(phi, 10); Assertions.assertThrows( ExFailure.class, @@ -73,7 +76,7 @@ void failsOnDoubleAllocation() { @Test void allocatesAndReadsEmptyBytes() { - final int idx = HeapsTest.HEAPS.malloc(new PhFake(), 5); + final int idx = HeapsTest.HEAPS.malloc(new HeapsTest.PhFake(), 5); MatcherAssert.assertThat( AtCompositeTest.TO_ADD_MESSAGE, HeapsTest.HEAPS.read(idx, 0, 5), @@ -84,7 +87,7 @@ void allocatesAndReadsEmptyBytes() { @Test void writesAndReads() { - final int idx = HeapsTest.HEAPS.malloc(new PhFake(), 5); + final int idx = HeapsTest.HEAPS.malloc(new HeapsTest.PhFake(), 5); final byte[] bytes = {1, 2, 3, 4, 5}; HeapsTest.HEAPS.write(idx, 0, bytes); MatcherAssert.assertThat( @@ -99,7 +102,7 @@ void writesAndReads() { void failsOnWriteToEmptyBlock() { Assertions.assertThrows( ExFailure.class, - () -> HeapsTest.HEAPS.write(new PhFake().hashCode(), 0, new byte[] {0x01}), + () -> HeapsTest.HEAPS.write(new HeapsTest.PhFake().hashCode(), 0, new byte[] {0x01}), AtCompositeTest.TO_ADD_MESSAGE ); } @@ -108,14 +111,14 @@ void failsOnWriteToEmptyBlock() { void failsOnReadFromEmptyBlock() { Assertions.assertThrows( ExFailure.class, - () -> HeapsTest.HEAPS.read(new PhFake().hashCode(), 0, 1), + () -> HeapsTest.HEAPS.read(new HeapsTest.PhFake().hashCode(), 0, 1), AtCompositeTest.TO_ADD_MESSAGE ); } @Test void failsOnReadIfOutOfBounds() { - final int idx = HeapsTest.HEAPS.malloc(new PhFake(), 2); + final int idx = HeapsTest.HEAPS.malloc(new HeapsTest.PhFake(), 2); Assertions.assertThrows( ExFailure.class, () -> HeapsTest.HEAPS.read(idx, 1, 3), @@ -125,7 +128,7 @@ void failsOnReadIfOutOfBounds() { @Test void readsByOffsetAndLength() { - final int idx = HeapsTest.HEAPS.malloc(new PhFake(), 5); + final int idx = HeapsTest.HEAPS.malloc(new HeapsTest.PhFake(), 5); HeapsTest.HEAPS.write(idx, 0, new byte[] {1, 2, 3, 4, 5}); MatcherAssert.assertThat( AtCompositeTest.TO_ADD_MESSAGE, @@ -136,7 +139,7 @@ void readsByOffsetAndLength() { @Test void failsOnWriteMoreThanAllocated() { - final int idx = HeapsTest.HEAPS.malloc(new PhFake(), 2); + final int idx = HeapsTest.HEAPS.malloc(new HeapsTest.PhFake(), 2); final byte[] bytes = {1, 2, 3, 4, 5}; Assertions.assertThrows( ExFailure.class, @@ -148,7 +151,7 @@ void failsOnWriteMoreThanAllocated() { @Test void failsToWriteMoreThanAllocatedWithOffset() { - final int idx = HeapsTest.HEAPS.malloc(new PhFake(), 3); + final int idx = HeapsTest.HEAPS.malloc(new HeapsTest.PhFake(), 3); final byte[] bytes = {1, 2, 3}; Assertions.assertThrows( ExFailure.class, @@ -160,7 +163,7 @@ void failsToWriteMoreThanAllocatedWithOffset() { @Test void concatsOnWriteLessThanAllocated() { - final int idx = HeapsTest.HEAPS.malloc(new PhFake(), 5); + final int idx = HeapsTest.HEAPS.malloc(new HeapsTest.PhFake(), 5); HeapsTest.HEAPS.write(idx, 0, new byte[] {1, 1, 3, 4, 5}); HeapsTest.HEAPS.write(idx, 2, new byte[] {2, 2}); MatcherAssert.assertThat( @@ -173,7 +176,7 @@ void concatsOnWriteLessThanAllocated() { @Test void freesSuccessfully() { - final int idx = HeapsTest.HEAPS.malloc(new PhFake(), 5); + final int idx = HeapsTest.HEAPS.malloc(new HeapsTest.PhFake(), 5); HeapsTest.HEAPS.free(idx); Assertions.assertThrows( ExFailure.class, @@ -186,7 +189,7 @@ void freesSuccessfully() { void failsOnClearingEmptyBlock() { Assertions.assertThrows( ExFailure.class, - () -> HeapsTest.HEAPS.free(new PhFake().hashCode()), + () -> HeapsTest.HEAPS.free(new HeapsTest.PhFake().hashCode()), AtCompositeTest.TO_ADD_MESSAGE ); } @@ -195,14 +198,14 @@ void failsOnClearingEmptyBlock() { void throwsOnGettingSizeOfEmptyBlock() { Assertions.assertThrows( ExFailure.class, - () -> HeapsTest.HEAPS.size(new PhFake().hashCode()), + () -> HeapsTest.HEAPS.size(new HeapsTest.PhFake().hashCode()), "Heaps should throw an exception if trying to get size on an empty block, but it didn't" ); } @Test void returnsValidSize() { - final int idx = HeapsTest.HEAPS.malloc(new PhFake(), 5); + final int idx = HeapsTest.HEAPS.malloc(new HeapsTest.PhFake(), 5); MatcherAssert.assertThat( "Heaps should return valid size of allocated block, but it didn't", HeapsTest.HEAPS.size(idx), @@ -213,7 +216,7 @@ void returnsValidSize() { @Test void throwsOnChangingSizeToNegative() { - final int idx = HeapsTest.HEAPS.malloc(new PhFake(), 5); + final int idx = HeapsTest.HEAPS.malloc(new HeapsTest.PhFake(), 5); Assertions.assertThrows( ExFailure.class, () -> HeapsTest.HEAPS.resize(idx, -1), @@ -226,14 +229,14 @@ void throwsOnChangingSizeToNegative() { void throwsOnChangeSizeOfEmtpyBlock() { Assertions.assertThrows( ExFailure.class, - () -> HeapsTest.HEAPS.resize(new PhFake().hashCode(), 10), + () -> HeapsTest.HEAPS.resize(new HeapsTest.PhFake().hashCode(), 10), "Heaps should throw an exception on changing size of empty block, but it didn't" ); } @Test void increasesSizeSuccessfully() { - final int idx = HeapsTest.HEAPS.malloc(new PhFake(), 5); + final int idx = HeapsTest.HEAPS.malloc(new HeapsTest.PhFake(), 5); final byte[] bytes = {1, 2, 3, 4, 5}; HeapsTest.HEAPS.write(idx, 0, bytes); HeapsTest.HEAPS.resize(idx, 7); @@ -247,7 +250,7 @@ void increasesSizeSuccessfully() { @Test void decreasesSizeSuccessfully() { - final int idx = HeapsTest.HEAPS.malloc(new PhFake(), 5); + final int idx = HeapsTest.HEAPS.malloc(new HeapsTest.PhFake(), 5); final byte[] bytes = {1, 2, 3, 4, 5}; HeapsTest.HEAPS.write(idx, 0, bytes); HeapsTest.HEAPS.resize(idx, 3); @@ -261,7 +264,7 @@ void decreasesSizeSuccessfully() { @Test void returnsValidSizeAfterDecreasing() { - final int idx = HeapsTest.HEAPS.malloc(new PhFake(), 5); + final int idx = HeapsTest.HEAPS.malloc(new HeapsTest.PhFake(), 5); final byte[] bytes = {1, 2, 3, 4, 5}; HeapsTest.HEAPS.write(idx, 0, bytes); HeapsTest.HEAPS.resize(idx, 3); @@ -273,4 +276,27 @@ void returnsValidSizeAfterDecreasing() { HeapsTest.HEAPS.free(idx); } + /** + * Fake object, mostly for unit tests. + * + * @since 0.29 + */ + private static final class PhFake extends PhDefault { + /** + * Ctor. + */ + PhFake() { + this(() -> Phi.Φ); + } + + /** + * Ctor. + * @param sup The function to return the real object + */ + @SuppressWarnings("PMD.ConstructorOnlyInitializesOrCallOtherConstructors") + PhFake(final Supplier sup) { + this.add("args", new AtVoid("args")); + this.add("φ", new AtComposite(this, rho -> sup.get())); + } + } } diff --git a/eo-runtime/src/main/java/org/eolang/AtEnvelope.java b/eo-runtime/src/test/java/org/eolang/AtEnvelope.java similarity index 87% rename from eo-runtime/src/main/java/org/eolang/AtEnvelope.java rename to eo-runtime/src/test/java/org/eolang/AtEnvelope.java index 4d770a652b..6b28f01286 100644 --- a/eo-runtime/src/main/java/org/eolang/AtEnvelope.java +++ b/eo-runtime/src/test/java/org/eolang/AtEnvelope.java @@ -30,7 +30,8 @@ * @since 0.36.0 * @checkstyle DesignForExtensionCheck (100 lines) */ -public abstract class AtEnvelope implements Attr { +@SuppressWarnings({"JTCOP.RuleAllTestsHaveProductionClass", "JTCOP.RuleCorrectTestName"}) +abstract class AtEnvelope implements Attr { /** * Original attribute. */ @@ -40,7 +41,7 @@ public abstract class AtEnvelope implements Attr { * Ctor. * @param attr Attribute */ - public AtEnvelope(final Attr attr) { + AtEnvelope(final Attr attr) { this.origin = attr; } @@ -55,7 +56,7 @@ public Phi get() { } @Override - public boolean put(final Phi phi) { - return this.origin.put(phi); + public void put(final Phi phi) { + this.origin.put(phi); } } diff --git a/eo-runtime/src/main/java/org/eolang/AtSimple.java b/eo-runtime/src/test/java/org/eolang/AtSimple.java similarity index 86% rename from eo-runtime/src/main/java/org/eolang/AtSimple.java rename to eo-runtime/src/test/java/org/eolang/AtSimple.java index 9d2616f33f..7fbd4ae200 100644 --- a/eo-runtime/src/main/java/org/eolang/AtSimple.java +++ b/eo-runtime/src/test/java/org/eolang/AtSimple.java @@ -32,11 +32,16 @@ * * @since 0.1 */ -public final class AtSimple extends AtEnvelope { +@SuppressWarnings({ + "JTCOP.RuleAllTestsHaveProductionClass", + "JTCOP.RuleCorrectTestName", + "JTCOP.RuleInheritanceInTests" +}) +final class AtSimple extends AtEnvelope { /** * Ctor. */ - public AtSimple() { + AtSimple() { this(Phi.Φ); } @@ -44,7 +49,7 @@ public AtSimple() { * Ctor. * @param object Object that attribute keeps */ - public AtSimple(final Phi object) { + AtSimple(final Phi object) { super(new AtComposite(object, arg -> object)); } } diff --git a/eo-runtime/src/test/java/org/eolang/PhDefaultTest.java b/eo-runtime/src/test/java/org/eolang/PhDefaultTest.java index dee7ea8e9d..e50e67d62b 100644 --- a/eo-runtime/src/test/java/org/eolang/PhDefaultTest.java +++ b/eo-runtime/src/test/java/org/eolang/PhDefaultTest.java @@ -493,7 +493,7 @@ private static class Rnd extends PhDefault { "φ", new AtComposite( this, - self -> new ToPhi(new SecureRandom().nextDouble()) + self -> new Data.ToPhi(new SecureRandom().nextDouble()) ) ); } From 0e3ec4663c5e5a57d7a83ba7bbb2a8a27db17c39 Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Fri, 10 Jan 2025 17:16:24 +0300 Subject: [PATCH 2/2] bug(#3790): put --- eo-runtime/src/main/java/org/eolang/PhDefault.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/eo-runtime/src/main/java/org/eolang/PhDefault.java b/eo-runtime/src/main/java/org/eolang/PhDefault.java index d8ae1d4be9..b8d97facd3 100644 --- a/eo-runtime/src/main/java/org/eolang/PhDefault.java +++ b/eo-runtime/src/main/java/org/eolang/PhDefault.java @@ -98,7 +98,7 @@ public PhDefault() { */ public PhDefault(final byte[] dta) { this.data = Optional.ofNullable(dta); - this.attrs = Map.of(Attr.RHO, new AtRho()); + this.attrs = PhDefault.defaults(); this.order = new HashMap<>(0); } @@ -302,6 +302,16 @@ private String oname() { return txt; } + /** + * Default attributes hash map with RHO attribute put. + * @return Default attributes hash map + */ + private static Map defaults() { + final Map attrs = new HashMap<>(0); + attrs.put(Attr.RHO, new AtRho()); + return attrs; + } + /** * Log debug message for PhDefault. * @param msg Message to log