-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
581 additions
and
498 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
174 changes: 174 additions & 0 deletions
174
eo-runtime/src/main/java/EOorg/EOeolang/EOcage$EOnew.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2016-2024 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 EOorg.EOeolang; | ||
|
||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import org.eolang.AtSimple; | ||
import org.eolang.Atom; | ||
import org.eolang.Attr; | ||
import org.eolang.Data; | ||
import org.eolang.ExFailure; | ||
import org.eolang.PhDefault; | ||
import org.eolang.PhTracedLocator; | ||
import org.eolang.PhWrite; | ||
import org.eolang.Phi; | ||
import org.eolang.Term; | ||
import org.eolang.XmirObject; | ||
|
||
/** | ||
* Cage.alloc object. | ||
* @since 0.36.0 | ||
* @checkstyle TypeNameCheck (5 lines) | ||
*/ | ||
@XmirObject(oname = "cage.new") | ||
public final class EOcage$EOnew extends PhDefault implements Atom { | ||
/** | ||
* Locator calculator. | ||
*/ | ||
private static final AtomicInteger LOCATOR = new AtomicInteger(0); | ||
|
||
/** | ||
* Cage for objects. | ||
*/ | ||
private static final ConcurrentHashMap<Integer, Phi> CAGES = new ConcurrentHashMap<>(0); | ||
|
||
/** | ||
* Ctor. | ||
* @param sigma Sigma | ||
*/ | ||
EOcage$EOnew(final Phi sigma) { | ||
super(sigma); | ||
this.add("it", new EOcage$EOnew.AtEncaged()); | ||
this.add( | ||
"encage", | ||
new AtSimple( | ||
new PhWrite( | ||
this, | ||
"it", | ||
rho -> new Data.ToPhi(true) | ||
) | ||
) | ||
); | ||
} | ||
|
||
@Override | ||
public Phi lambda() throws Exception { | ||
return this.take("it"); | ||
} | ||
|
||
/** | ||
* Attribute that stores object. | ||
* @since 0.36.0 | ||
*/ | ||
private static class AtEncaged implements Attr { | ||
/** | ||
* Locator of encaged object. | ||
*/ | ||
private Integer locator; | ||
|
||
/** | ||
* Form of the stored object. | ||
*/ | ||
private String forma; | ||
|
||
/** | ||
* Ctor. | ||
*/ | ||
AtEncaged() { | ||
this(null, null); | ||
} | ||
|
||
/** | ||
* Ctor for copying. | ||
* @param locator Locator of object in memory | ||
* @param form The form of the object | ||
*/ | ||
AtEncaged(final Integer locator, final String form) { | ||
this.locator = locator; | ||
this.forma = form; | ||
} | ||
|
||
@Override | ||
public Attr copy(final Phi self) { | ||
return new EOcage$EOnew.AtEncaged(this.locator, this.forma); | ||
} | ||
|
||
@Override | ||
public Phi get() { | ||
if (this.locator == null || !EOcage$EOnew.CAGES.containsKey(this.locator)) { | ||
throw new ExFailure( | ||
"There's no object in storage, can't read" | ||
); | ||
} | ||
return new PhTracedLocator(EOcage$EOnew.CAGES.get(this.locator), this.locator); | ||
} | ||
|
||
@Override | ||
public void put(final Phi phi) { | ||
if (this.forma == null) { | ||
this.forma = phi.forma(); | ||
} else if (!this.forma.equals(phi.forma())) { | ||
throw new ExFailure( | ||
"Can't write an object formed by %s because object formed by %s was saved before", | ||
phi.forma(), | ||
this.forma | ||
); | ||
} | ||
if (this.locator == null) { | ||
synchronized (EOcage$EOnew.LOCATOR) { | ||
this.locator = EOcage$EOnew.LOCATOR.incrementAndGet(); | ||
} | ||
} | ||
EOcage$EOnew.CAGES.put(this.locator, phi); | ||
} | ||
|
||
@Override | ||
public String φTerm() { | ||
final String txt; | ||
if (this.locator == null || !EOcage$EOnew.CAGES.containsKey(this.locator)) { | ||
txt = Term.EMPTY; | ||
} else { | ||
txt = EOcage$EOnew.CAGES.get(this.locator).φTerm(); | ||
} | ||
return txt; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
final String txt; | ||
if (this.locator == null || !EOcage$EOnew.CAGES.containsKey(this.locator)) { | ||
txt = Term.EMPTY; | ||
} else { | ||
txt = EOcage$EOnew.CAGES.get(this.locator).toString(); | ||
} | ||
return txt; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
a3fa8c0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Puzzle
2931-0ee2db02
disappeared fromeo-runtime/src/test/eo/org/eolang/cage-tests.eo
), that's why I closed #3001. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.a3fa8c0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Puzzle
2931-0ed69df1
disappeared fromeo-runtime/src/test/eo/org/eolang/runtime-tests.eo
), that's why I closed #3021. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.