OpenSeSim/src/opensesim/world/AbstractAsset.java

161 lines
4.4 KiB
Java
Raw Normal View History

2018-11-30 12:27:41 +01:00
/*
* Copyright (c) 2018, 7u83 <7u83@mail.ru>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package opensesim.world;
2018-11-30 12:27:41 +01:00
2018-12-26 13:11:35 +01:00
import java.text.DecimalFormat;
2018-11-30 12:27:41 +01:00
import javax.swing.JPanel;
2018-12-05 18:28:40 +01:00
import opensesim.sesim.interfaces.GetJson;
2018-11-30 12:27:41 +01:00
import org.json.JSONObject;
/**
*
* @author 7u83 <7u83@mail.ru>
*/
2018-12-29 14:31:47 +01:00
public abstract class AbstractAsset implements GetJson, Asset {
2018-12-04 01:36:00 +01:00
GodWorld world;
2018-12-04 01:36:00 +01:00
2018-11-30 12:27:41 +01:00
private String symbol;
private String name;
private String description;
2018-12-26 13:11:35 +01:00
private int decimals;
private double decimals_df;
private DecimalFormat formatter;
2018-11-30 12:27:41 +01:00
2018-12-27 17:52:04 +01:00
public DecimalFormat getFormatter() {
return formatter;
}
2018-11-30 12:27:41 +01:00
/**
* Constructor
2018-12-05 18:28:40 +01:00
*
2018-12-04 01:36:00 +01:00
* @param world
* @param cfg
2018-11-30 12:27:41 +01:00
*/
public AbstractAsset(GodWorld world, JSONObject cfg) {
2018-12-05 18:28:40 +01:00
if (world == null) {
2018-12-04 01:36:00 +01:00
return;
2018-12-05 18:28:40 +01:00
}
2018-12-04 01:36:00 +01:00
symbol = cfg.getString("symbol");
name = cfg.getString("name");
2018-12-26 13:11:35 +01:00
setDecimals(cfg.optInt("decimals", 0));
2018-12-05 18:28:40 +01:00
2018-12-04 01:36:00 +01:00
this.world = world;
2018-11-30 12:27:41 +01:00
}
2018-12-26 13:11:35 +01:00
public AbstractAsset() {
2018-12-08 15:00:35 +01:00
}
2018-11-30 12:27:41 +01:00
2018-12-26 13:11:35 +01:00
public double roundToDecimals(double val) {
return Math.floor(val * decimals_df) / decimals_df;
}
2018-12-29 14:31:47 +01:00
protected final void setDecimals(int n) {
2018-12-26 13:11:35 +01:00
decimals = n;
decimals_df = Math.pow(10, n);
// create formatter string
String fs = "#";
if (n > 0) {
fs = fs + "0.";
for (int i = 0; i < n; i++) {
fs = fs + "0";
}
}
// create fromatter from string
formatter = new DecimalFormat(fs);
}
2018-11-30 12:27:41 +01:00
2018-12-29 21:02:01 +01:00
@Override
2018-12-08 15:00:35 +01:00
public final int getDecimals() {
2018-11-30 12:27:41 +01:00
return decimals;
}
2018-12-26 13:11:35 +01:00
2018-12-29 21:02:01 +01:00
@Override
2018-12-26 13:11:35 +01:00
public abstract String getTypeName();
2018-11-30 12:27:41 +01:00
2018-12-08 12:17:53 +01:00
protected void setDescription(String description) {
2018-11-30 12:27:41 +01:00
this.description = description;
}
2018-12-29 21:02:01 +01:00
@Override
2018-12-08 15:00:35 +01:00
public final String getSymbol() {
2018-11-30 12:27:41 +01:00
return symbol;
}
2018-12-26 13:11:35 +01:00
protected final void setSymbol(String symbol) {
this.symbol = symbol;
2018-12-08 15:00:35 +01:00
}
2018-11-30 12:27:41 +01:00
2018-12-08 15:00:35 +01:00
public final String getName() {
2018-11-30 12:27:41 +01:00
return name;
}
2018-12-26 13:11:35 +01:00
protected final void setName(String name) {
this.name = name;
2018-12-08 15:00:35 +01:00
}
2018-11-30 12:27:41 +01:00
public String getDescription() {
return description;
}
2018-12-04 01:36:00 +01:00
2018-12-29 21:02:01 +01:00
@Override
2018-12-04 01:36:00 +01:00
public boolean isCurrency() {
2018-11-30 12:27:41 +01:00
return false;
}
2018-12-04 01:36:00 +01:00
2018-12-29 21:02:01 +01:00
@Override
2018-12-04 01:36:00 +01:00
public boolean isAsset() {
2018-11-30 12:27:41 +01:00
return false;
}
2018-12-04 01:36:00 +01:00
public static final String JSON_ID = "id";
public static final String JSON_CLASS = "class";
2018-11-30 12:27:41 +01:00
public static final String JSON_SYMBOL = "symbol";
public static final String JSON_NAME = "name";
public static final String JSON_DESCRIPTION = "description";
public static final String JSON_DECIMALS = "decimals";
public static final int DECIMALS_DEFAULT = 2;
2018-12-05 18:28:40 +01:00
@Override
public JSONObject getJson() {
2018-11-30 12:27:41 +01:00
JSONObject cfg = new JSONObject();
cfg.put(GodWorld.JKEYS.ASSET_TYPE, this.getClass().getName());
2018-12-05 18:28:40 +01:00
2018-11-30 12:27:41 +01:00
cfg.put(AbstractAsset.JSON_SYMBOL, this.getSymbol());
cfg.put(AbstractAsset.JSON_DECIMALS, this.getDecimals());
cfg.put(AbstractAsset.JSON_NAME, this.getName());
cfg.put(AbstractAsset.JSON_DESCRIPTION, this.getDescription());
2018-12-05 18:28:40 +01:00
2018-11-30 12:27:41 +01:00
return cfg;
}
2018-12-04 01:36:00 +01:00
public JPanel getEditGui() {
2018-11-30 12:27:41 +01:00
return null;
}
}