-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
m-m-m/bean#7: getReadOnly() and WritableBean constructor
- Loading branch information
Showing
1 changed file
with
111 additions
and
92 deletions.
There are no files selected for viewing
203 changes: 111 additions & 92 deletions
203
bean/src/main/java/io/github/mmm/entity/bean/AdvancedEntityBean.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 |
---|---|---|
@@ -1,92 +1,111 @@ | ||
/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0 | ||
* http://www.apache.org/licenses/LICENSE-2.0 */ | ||
package io.github.mmm.entity.bean; | ||
|
||
import io.github.mmm.bean.AdvancedBean; | ||
import io.github.mmm.bean.Bean; | ||
import io.github.mmm.bean.BeanClass; | ||
import io.github.mmm.bean.StandardPropertyBuilders; | ||
import io.github.mmm.entity.bean.impl.EntityPropertyBuildersImpl; | ||
import io.github.mmm.entity.id.Id; | ||
import io.github.mmm.entity.id.LongVersionId; | ||
import io.github.mmm.entity.property.builder.EntityPropertyBuilders; | ||
import io.github.mmm.entity.property.id.IdProperty; | ||
import io.github.mmm.entity.property.id.PkProperty; | ||
import io.github.mmm.property.PropertyMetadata; | ||
|
||
/** | ||
* Implementation of {@link EntityBean} as simple {@link Bean}. | ||
* | ||
* @since 1.0.0 | ||
*/ | ||
public class AdvancedEntityBean extends AdvancedBean implements EntityBean { | ||
|
||
/** The {@link IdProperty property} with the {@link Id primary key}. */ | ||
public final PkProperty Id; | ||
|
||
/** | ||
* The constructor. | ||
*/ | ||
public AdvancedEntityBean() { | ||
|
||
this(null, null); | ||
} | ||
|
||
/** | ||
* The constructor. | ||
* | ||
* @param pkProperty the {@link #Id() ID property}. | ||
*/ | ||
public AdvancedEntityBean(PkProperty pkProperty) { | ||
|
||
this(null, pkProperty); | ||
} | ||
|
||
/** | ||
* The constructor. | ||
* | ||
* @param type the {@link #getType() type}. | ||
*/ | ||
public AdvancedEntityBean(BeanClass type) { | ||
|
||
this(type, null); | ||
} | ||
|
||
/** | ||
* The constructor. | ||
* | ||
* @param type the {@link #getType() type}. | ||
* @param pkProperty the {@link #Id() ID property}. | ||
*/ | ||
private AdvancedEntityBean(BeanClass type, PkProperty pkProperty) { | ||
|
||
super(type); | ||
if (pkProperty == null) { | ||
// default | ||
Id<?> id = LongVersionId.getEmpty().withEntityType(getClass()); | ||
pkProperty = new PkProperty(id, PropertyMetadata.of(this)); | ||
} else { | ||
assert pkProperty.getName().equals(PkProperty.NAME); | ||
} | ||
this.Id = add(pkProperty); | ||
} | ||
|
||
@Override | ||
public PkProperty Id() { | ||
|
||
return this.Id; | ||
} | ||
|
||
@Override | ||
protected EntityPropertyBuilders add() { | ||
|
||
return (EntityPropertyBuilders) super.add(); | ||
} | ||
|
||
@Override | ||
protected StandardPropertyBuilders createPropertyBuilders() { | ||
|
||
return new EntityPropertyBuildersImpl(this); | ||
} | ||
|
||
} | ||
/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0 | ||
* http://www.apache.org/licenses/LICENSE-2.0 */ | ||
package io.github.mmm.entity.bean; | ||
|
||
import io.github.mmm.bean.AdvancedBean; | ||
import io.github.mmm.bean.Bean; | ||
import io.github.mmm.bean.BeanClass; | ||
import io.github.mmm.bean.StandardPropertyBuilders; | ||
import io.github.mmm.bean.WritableBean; | ||
import io.github.mmm.entity.bean.impl.EntityPropertyBuildersImpl; | ||
import io.github.mmm.entity.id.Id; | ||
import io.github.mmm.entity.id.LongVersionId; | ||
import io.github.mmm.entity.property.builder.EntityPropertyBuilders; | ||
import io.github.mmm.entity.property.id.IdProperty; | ||
import io.github.mmm.entity.property.id.PkProperty; | ||
import io.github.mmm.property.PropertyMetadata; | ||
|
||
/** | ||
* Implementation of {@link EntityBean} as simple {@link Bean}. | ||
* | ||
* @since 1.0.0 | ||
*/ | ||
public class AdvancedEntityBean extends AdvancedBean implements EntityBean { | ||
|
||
/** The {@link IdProperty property} with the {@link Id primary key}. */ | ||
public final PkProperty Id; | ||
|
||
/** | ||
* The constructor. | ||
*/ | ||
public AdvancedEntityBean() { | ||
|
||
this(null, null, null); | ||
} | ||
|
||
/** | ||
* The constructor. | ||
* | ||
* @param writable the {@link WritableBean} to wrap as {@link #isReadOnly() read-only} bean or {@code null} to create | ||
* a mutable bean. | ||
*/ | ||
public AdvancedEntityBean(WritableBean writable) { | ||
|
||
this(writable, null, null); | ||
} | ||
|
||
/** | ||
* The constructor. | ||
* | ||
* @param pkProperty the {@link #Id() ID property}. | ||
*/ | ||
public AdvancedEntityBean(PkProperty pkProperty) { | ||
|
||
this(null, null, pkProperty); | ||
} | ||
|
||
/** | ||
* The constructor. | ||
* | ||
* @param type the {@link #getType() type}. | ||
*/ | ||
public AdvancedEntityBean(BeanClass type) { | ||
|
||
this(null, type, null); | ||
} | ||
|
||
/** | ||
* The constructor. | ||
* | ||
* @param writable the {@link WritableBean} to wrap as {@link #isReadOnly() read-only} bean or {@code null} to create | ||
* a mutable bean. | ||
* @param type the {@link #getType() type}. | ||
* @param pkProperty the {@link #Id() ID property}. | ||
*/ | ||
private AdvancedEntityBean(WritableBean writable, BeanClass type, PkProperty pkProperty) { | ||
|
||
super(writable, type); | ||
if (pkProperty == null) { | ||
if (writable == null) { | ||
// default | ||
Id<?> id = LongVersionId.getEmpty().withEntityType(getClass()); | ||
pkProperty = new PkProperty(id, PropertyMetadata.of(this)); | ||
} else { | ||
pkProperty = ((AdvancedEntityBean) writable).Id; | ||
} | ||
} else { | ||
assert pkProperty.getName().equals(PkProperty.NAME); | ||
assert (writable == null); | ||
} | ||
this.Id = add(pkProperty); | ||
} | ||
|
||
@Override | ||
public PkProperty Id() { | ||
|
||
return this.Id; | ||
} | ||
|
||
@Override | ||
protected EntityPropertyBuilders add() { | ||
|
||
return (EntityPropertyBuilders) super.add(); | ||
} | ||
|
||
@Override | ||
protected StandardPropertyBuilders createPropertyBuilders() { | ||
|
||
return new EntityPropertyBuildersImpl(this); | ||
} | ||
|
||
} |