Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,19 +1,74 @@
package org.incenp.linkml.core.samples.refinhslot;

/**
* An example of a class that is used in a “refined” slot.
* <p>
* This class is used in the {@link Foo} class. Some of the classes that are
* derived from <code>Foo</code> uses derived classes instead.
*/
import java.net.URI;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.incenp.linkml.core.annotations.Converter;
import org.incenp.linkml.core.annotations.ExtensionHolder;
import org.incenp.linkml.core.annotations.Identifier;
import org.incenp.linkml.core.annotations.Inlined;
import org.incenp.linkml.core.annotations.LinkURI;
import org.incenp.linkml.core.annotations.Required;
import org.incenp.linkml.core.annotations.SlotName;
import org.incenp.linkml.core.annotations.TypeDesignator;
import org.incenp.linkml.core.CurieConverter;

@LinkURI("https://w3id.org/linkml/tests/refined_derived_slots/Bar")
public class Bar {

@LinkURI("https://w3id.org/linkml/tests/refined_derived_slots/name")
private String name;

public void setName(String name) {
this.name = name;
}

public String getName() {
return name;
return this.name;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
Object o;
sb.append("Bar(");
if ( (o = this.getName()) != null ) {
sb.append("name=");
sb.append(o);
sb.append(",");
}
sb.append(")");
return sb.toString();
}

@Override
public boolean equals(final Object o) {
if ( o == this ) return true;
if ( !(o instanceof Bar) ) return false;
final Bar other = (Bar) o;
if ( !other.canEqual((Object) this)) return false;
final Object this$name = this.getName();
final Object other$name = other.getName();
if ( this$name == null ? other$name != null : !this$name.equals(other$name)) return false;
return true;
}

protected boolean canEqual(final Object other) {
return other instanceof Bar;
}

public void setName(String value) {
name = value;
@Override
public int hashCode() {
final int PRIME = 59;
int result = 1;
final Object $name = this.getName();
result = result * PRIME + ($name == null ? 43 : $name.hashCode());
return result;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,81 @@
package org.incenp.linkml.core.samples.refinhslot;

/**
* First derived class from Bar.
* <p>
* This class is used, instead of its parent <code>Bar</code> in
* {@link FirstDerivedFoo}.
*/
import java.net.URI;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.incenp.linkml.core.annotations.Converter;
import org.incenp.linkml.core.annotations.ExtensionHolder;
import org.incenp.linkml.core.annotations.Identifier;
import org.incenp.linkml.core.annotations.Inlined;
import org.incenp.linkml.core.annotations.LinkURI;
import org.incenp.linkml.core.annotations.Required;
import org.incenp.linkml.core.annotations.SlotName;
import org.incenp.linkml.core.annotations.TypeDesignator;
import org.incenp.linkml.core.CurieConverter;

@LinkURI("https://w3id.org/linkml/tests/refined_derived_slots/FirstDerivedBar")
public class FirstDerivedBar extends Bar {

private int length;
@LinkURI("https://w3id.org/linkml/tests/refined_derived_slots/length")
private Integer length;

public void setLength(Integer length) {
this.length = length;
}

public Integer getLength() {
return this.length;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
Object o;
sb.append("FirstDerivedBar(");
if ( (o = this.getLength()) != null ) {
sb.append("length=");
sb.append(o);
sb.append(",");
}
if ( (o = this.getName()) != null ) {
sb.append("name=");
sb.append(o);
sb.append(",");
}
sb.append(")");
return sb.toString();
}

@Override
public boolean equals(final Object o) {
if ( o == this ) return true;
if ( !(o instanceof FirstDerivedBar) ) return false;
final FirstDerivedBar other = (FirstDerivedBar) o;
if ( !other.canEqual((Object) this)) return false;
if ( !super.equals(o) ) return false;

final Object this$length = this.getLength();
final Object other$length = other.getLength();
if ( this$length == null ? other$length != null : !this$length.equals(other$length)) return false;
return true;
}

public int getLength() {
return length;
protected boolean canEqual(final Object other) {
return other instanceof FirstDerivedBar;
}

public void setLength(int value) {
length = value;
@Override
public int hashCode() {
final int PRIME = 59;
int result = super.hashCode();
final Object $length = this.getLength();
result = result * PRIME + ($length == null ? 43 : $length.hashCode());
return result;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,36 @@
package org.incenp.linkml.core.samples.refinhslot;

import java.net.URI;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* An example of a class that refines the range of its slots to make them accept
* only a more specialised subclass.
*/
import org.incenp.linkml.core.annotations.Converter;
import org.incenp.linkml.core.annotations.ExtensionHolder;
import org.incenp.linkml.core.annotations.Identifier;
import org.incenp.linkml.core.annotations.Inlined;
import org.incenp.linkml.core.annotations.LinkURI;
import org.incenp.linkml.core.annotations.Required;
import org.incenp.linkml.core.annotations.SlotName;
import org.incenp.linkml.core.annotations.TypeDesignator;
import org.incenp.linkml.core.CurieConverter;

@LinkURI("https://w3id.org/linkml/tests/refined_derived_slots/FirstDerivedFoo")
public class FirstDerivedFoo extends Foo {

/*
* Overridden read accessor for the `bar` slot.
*
* We override it to ensure that it returns the more specialised subtype.
*/
@Override
public FirstDerivedBar getBar() {
// This cast is perfectly safe because the write accessor below guarantees that
// only a FirstDerivedBar object can be assigned to the slot.
return (FirstDerivedBar) super.getBar();
}

/*
* Overridden write accessor for the `bar` slot.
*
* We override it to add a runtime check to enforce the more specialised type
* constraint. We cannot prevent client code from trying to assign an object of
* the wrong type, but if that happens we can at least immediately throw an
* exception.
*/
public void setBar(FirstDerivedBar value) {
super.setBar(value);
}

@Override
public void setBar(Bar value) {
if ( !(value instanceof FirstDerivedBar) ) {
Expand All @@ -36,54 +39,17 @@ public void setBar(Bar value) {
super.setBar(value);
}

/*
* Overloaded write accessor for the `bar` slot.
*
* This accessor is not strictly necessary, but it makes it clearer that in this
* class, the value of the `bar` slot should be a `FirstDerivedBar`. It also
* allows to bypass the dynamic check in the normal accessor above, if the
* compiler already knows that the assigned value is a FirstDerivedBar.
*/
public void setBar(FirstDerivedBar value) {
super.setBar(value);
}

/*
* Overridden “Standard” read accessor.
*
* We override it to ensure it returns the more specialised subtype.
*
* Because the slot could be (and instead is, in this example) refined further
* in subclasses, we must still return a generic wildcard, so this accessor has
* the same limitation as the one it overrides in the `Foo` class: modifying the
* returned list requires an explicit cast into a non-wildcard form.
*/
@Override
@SuppressWarnings("unchecked")
public List<? extends FirstDerivedBar> getBars() {
// This cast should be safe IFF nobody explicitly modify the value returned by
// this accessor after casting it into a `List<Bar>`.
return (List<FirstDerivedBar>) super.getBars();
}

/*
* Overridden read accessor with optional creation of the list.
*
* We must override this accessor to ensure that the created list (if the list
* needs to be created) is using the more specialised type.
*/
@Override
public List<? extends FirstDerivedBar> getBars(boolean create) {
// We can delegate the logic to the parent
return super.getBars(FirstDerivedBar.class, create);
}

/*
* Overridden parameterised read accessor.
*
* We must override this accessor to add a runtime check that the given type
* parameter is compatible with the more specialised type.
*/
@Override
public <T extends Bar> List<T> getBars(Class<T> t) {
if ( !FirstDerivedBar.class.isAssignableFrom(t) ) {
Expand All @@ -92,12 +58,6 @@ public <T extends Bar> List<T> getBars(Class<T> t) {
return super.getBars(t);
}

/*
* Overridden parameterised read accessor with optional creation of the list.
*
* Same as above: we must override this accessor to add a runtime check on the
* type parameter.
*/
@Override
public <T extends Bar> List<T> getBars(Class<T> t, boolean create) {
if ( !FirstDerivedBar.class.isAssignableFrom(t) ) {
Expand All @@ -106,20 +66,56 @@ public <T extends Bar> List<T> getBars(Class<T> t, boolean create) {
return super.getBars(t, create);
}

/*
* Overridden “standard” write accessor.
*
* We must override this accessor to include a runtime check. The check must be
* performed on all list items.
*/
@Override
public void setBars(List<? extends Bar> value) {
for ( Bar b : value ) {
if ( !(b instanceof FirstDerivedBar) ) {
throw new IllegalArgumentException("Invalid bars value");
if ( value != null ) {
for ( Bar item : value ) {
if ( !(item instanceof FirstDerivedBar) ) {
throw new IllegalArgumentException("Invalid bars value");
}
}
}
super.setBars(value);
}

}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
Object o;
sb.append("FirstDerivedFoo(");
if ( (o = this.getBar()) != null ) {
sb.append("bar=");
sb.append(o);
sb.append(",");
}
if ( (o = this.getBars()) != null ) {
sb.append("bars=");
sb.append(o);
sb.append(",");
}
sb.append(")");
return sb.toString();
}

@Override
public boolean equals(final Object o) {
if ( o == this ) return true;
if ( !(o instanceof FirstDerivedFoo) ) return false;
final FirstDerivedFoo other = (FirstDerivedFoo) o;
if ( !other.canEqual((Object) this)) return false;
if ( !super.equals(o) ) return false;

return true;
}

protected boolean canEqual(final Object other) {
return other instanceof FirstDerivedFoo;
}

@Override
public int hashCode() {
final int PRIME = 59;
int result = super.hashCode();
return result;
}
}
Loading
Loading