Jump to content

Recommended Posts

Posted (edited)

Alright as title says. Let's say we got an xml and we want to store objects which is a range of type. 

int[], ArrayLists, Strings e.t.c

 

Known ways:

 

1. Create ex. FastMap into FastMaps such as  FastMap<Integer, FastMap<String, int[]>>

which is complicated to put and retrieve objects.

 

2. Stats sets that contain other stats sets. We can add one statset into another and make a tree

 

Is there any other way and more efficient in order load items from xmls and  store them?

 

Ps. i would prefer an experienced person with java structure to answer and give other solutions that really make it easier and more dynamic.

Thanks

Edited by AccessDenied
Posted (edited)

A StatSet is nothing more than a extended HashMap with more methods, allowing you for a String key to retrieve any type of object. It allows easy fill on loading, for the cost of overhead. A StatSet shouldn't really be used because of the overhead generated by String key. You could for example move every single variable from L2PcInstance into a single StatsSet (which will allow you to drop every single getter and setter). Will you do it, knowing that you must .get( the Map when you could simply retrieve the correct boolean/int/String by yourself with a getter if that was written normally ? I don't think so.

 

Also, a FastMap got no use at all to load static data, you both don't need the potential concurrency from .shared(true), nor the ordered system. You just create overhead for nothing using a FastMap.

 

The proper way is to create you own class defining the object similar to... Everything else :P. For example, Castle is an object, filled by database on startup. On aCis I recently moved hardcoded or SQL data related to static stuff to XML, but basically the Castle object is filled with int, List<MercenaryTicket>, List<Integer>, etc etc...

 

So long answer short, create your own Object class holding properties, then either use a StatsSet to feed that object or put every single parameters in the constructor :

public class Castle
{
     int _castleId;
     String _name;

     // By feeding each parameters into constructor
     public Castle(int id, String name)
     {
          _castleId = id;
          _name = name;
     }


     // By feeding using a StatsSet
     public Castle(StatsSet data)
     {
          _castleId = data.getInteger("castleId");
          _name = data.getString("name");
     }
}

Obviously StatsSet is just more readable when you got 10, 15, 20 parameters to pass on the godamn constructor.

 

You can also feed your object using public setter. As you can see, the castle exemple simply got 2 properties when it normally got billions other (artifact id, circlet, list of mercenary tickets,...). The XML handles that scenario when parsing the XML :

...
if ("artifact".equalsIgnoreCase(cat.getNodeName()))
	castle.setArtifacts(cat.getAttributes().getNamedItem("val").getNodeValue());
...

And then, on Castle object :

public void setArtifacts(String idsToSplit)
{
	for (String idToSplit : idsToSplit.split(";"))
		_artifacts.add(Integer.parseInt(idToSplit));
}

That scenario is right for anything :

- ArmorSetTable uses ArmorSet as storage object.

- FishTable uses FishData

- CastleManager uses Castle

etc etc.

 

On aCis and latest L2J, you also have generic holders such as IntIntHolder which avoid to generate your own class but simply retrieve it from a id/value. IntIntHolder is useful to store skillId, skillLevel or itemId, price. Beware, IntIntHolder is still an object to create - it simply avoids you to write your own little inner classes when you need only to store a paired int/int.

Edited by Tryskell
Posted

A StatSet is nothing more than a extended HashMap with more methods, allowing you for a String key to retrieve any type of object. It allows easy fill on loading, for the cost of overhead. A StatSet shouldn't really be used because of the overhead generated by String key. You could for example move every single variable from L2PcInstance into a single StatsSet (which will allow you to drop every single getter and setter). Will you do it, knowing that you must .get( the Map when you could simply retrieve the correct boolean/int/String by yourself with a getter if that was written normally ? I don't think so.

 

Also, a FastMap got no use at all to load static data, you both don't need the potential concurrency from .shared(true), nor the ordered system. You just create overhead for nothing using a FastMap.

 

The proper way is to create you own class defining the object similar to... Everything else :P. For example, Castle is an object, filled by database on startup. On aCis I recently moved hardcoded or SQL data related to static stuff to XML, but basically the Castle object is filled with int, List<MercenaryTicket>, List<Integer>, etc etc...

 

So long answer short, create your own Object class holding properties, then either use a StatsSet to feed that object or put every single parameters in the constructor :

public class Castle
{
     int _castleId;
     String _name;

     // By feeding each parameters into constructor
     public Castle(int id, String name)
     {
          _castleId = id;
          _name = name;
     }


     // By feeding using a StatsSet
     public Castle(StatsSet data)
     {
          _castleId = data.getInteger("castleId");
          _name = data.getString("name");
     }
}

Obviously StatsSet is just more readable when you got 10, 15, 20 parameters to pass on the godamn constructor.

 

You can also feed your object using public setter. As you can see, the castle exemple simply got 2 properties when it normally got billions other (artifact id, circlet, list of mercenary tickets,...). The XML handles that scenario when parsing the XML :

...
if ("artifact".equalsIgnoreCase(cat.getNodeName()))
	castle.setArtifacts(cat.getAttributes().getNamedItem("val").getNodeValue());
...

And then, on Castle object :

public void setArtifacts(String idsToSplit)
{
	for (String idToSplit : idsToSplit.split(";"))
		_artifacts.add(Integer.parseInt(idToSplit));
}

That scenario is right for anything :

- ArmorSetTable uses ArmorSet as storage object.

- FishTable uses FishData

- CastleManager uses Castle

etc etc.

 

On aCis and latest L2J, you also have generic holders such as IntIntHolder which avoid to generate your own class but simply retrieve it from a id/value. IntIntHolder is useful to store skillId, skillLevel or itemId, price. Beware, IntIntHolder is still an object to create - it simply avoids you to write your own little inner classes when you need only to store a paired int/int.

 

 

First of all thanks for being serious. Afcourse Sset is more readable by user when you play with 10 different objects and variables example to store an Event Data which require let's say

1. team color (FastMap)

2. team name (FastMap)

3. map data which contain team position (FastMap into FastMap)

 

e.t.c and i don't know how many more variables can you store but since i made an event and i'm using FastMaps to store objects after 4 fastMaps it kinda start getting ridiculously hard. I mean only the idea

get(x).get(x).get(x).get(x) after 5 times it kill my brain.

 

Also i'm not really familiar when it come in Statset to load from xml. Everytime you load data from xml you have plenty of options and if they exist since you're using

 

 

 

if ("artifact".equalsIgnoreCase(cat.getNodeName()))

 

so since we don't know how many variables are loaded we can't set the statset. Maybe is complicated in my head thats why i asked any alternative idea. I want avoid making different classes

to make objects, i want keep all in 1 class and all in 1 method and make a simple style like    xxxx.getEvent(1).getData().getEventMap(1).getPositionForTeam(1); or something simple like that to avoid the 

mess i have right now with different classes. The idea behind this is to make a statsset that as you said extends HMs and contain other elements or other statsets inside if i'm right. The idea is pretty much easy.

I don't really want an example or anything i just wanted to know if the way i mention is better to be done with Sset or any other way in order to avoid making different classes and keep all in 1. 

 

Thanks again.

Posted

Are you trolling again or what? :lol:

No i'm actualy asking advice of someone more experienced to tell me if is ok to continue the idea i mentionted with the Sset or what. 

No searching for solution or examples just a yes or no and if is fine.

Posted

I answered you, StatsSet generates a overhead where basic properties don't. StatsSet should be kept only for data feeding.

 

And you obviously know what data you are loading for a raw stuff like Event. If you got a followup of get.get.get, it simply shows how terrible you structured your stuff. That's the sort of stuff to think BEFORE coding it.

 

An Event class probably has :

- int _id

- String _description

- List<L2PcInstance> _players

- List<Location> (or SpawnLocation) _spawnPoints

 

A Team class (can be edited for an extended enum) is different than a Event class. Don't melt everything. It probably has :

- int _id

- String _name

- int _color

 

etc etc.

Posted

It really depends what you want to do.

What should be taken into consideration:

- If object will change in time or loaded just once

- If it will be changed from multiple threads at the time

- How long(size, length) is it going to be

- What kind of access to it you want to have. It will be just fully iterated often, get(x), maybe you just want to take first index and then remove it?

 

Don't use FastList just because it is "fast".

 

If you want to load data from xml, i strongly suggest simple array(or obviously arrayList which might be later .trimToSize()).

 

Don't make FastMap<Integer, FastMap<String, int[]>>

Make:

ArrayList<MyContainer>();

class MyContainer
{
    private final int id;
    private final List<MySecondContainer> importantNameHere;
}

class MySecondContainer
{
    private final int String nameMaybe;
    private final int[] someKindOfData;
}

Thats most secure way.

- How long(size, length) is it going to be

Posted (edited)

It really depends what you want to do.

What should be taken into consideration:

- If object will change in time or loaded just once

- If it will be changed from multiple threads at the time

- How long(size, length) is it going to be

- What kind of access to it you want to have. It will be just fully iterated often, get(x), maybe you just want to take first index and then remove it?

 

Don't use FastList just because it is "fast".

 

If you want to load data from xml, i strongly suggest simple array(or obviously arrayList which might be later .trimToSize()).

 

Don't make FastMap<Integer, FastMap<String, int[]>>

Make:

ArrayList<MyContainer>();

class MyContainer
{
    private final int id;
    private final List<MySecondContainer> importantNameHere;
}

class MySecondContainer
{
    private final int String nameMaybe;
    private final int[] someKindOfData;
}

Thats most secure way.

- How long(size, length) is it going to be

 

Yeap kinda trying to make it like this

private final FastMap<Integer, ArrayList> _mapLocation = new FastMap<Integer, ArrayList>().shared();
	
	private class MapData
	{
		private String _mapName;
		private FastMap<Integer, ArrayList> mapLocation;
		
		private MapData(String mapName, FastMap<Integer, ArrayList> list)
		{
			_mapName = mapName;
			mapLocation = list;
		}
		
		public ArrayList getMapLocationByOwner(String owner)
		{
			ArrayList list = _mapLocation.get(owner);
			return list;
		}
		
		public String getMapName()
		{
			return _mapName;
		}
	}
	
	/**
	 * 
	 * @param id
	 * @return Selected map base on input id
	 */
	public MapData getMapById(int id)
	{
		if (_mapData.containsKey(id))
			return _mapData.get(id);
		else
			System.out.print("Error trying get null MAP");
		
		return null;
	}

Thats why i did until now but still is a bit fucked up..

 

And in ArrayList imma put HashMaps tho. Ps ignore the "_mapLocation.get(owner);"

Edited by AccessDenied
Posted

First of all, those locations are going to be just loaded and stay the same until server or xml restart yes? Then you dont need to make them .shared().

If you dont need shared, why bother to use FastMap at all, it is better to use HashMap.

 

Since you are querying the data by just .get(ID), map will be faster than list or set or other collections. 

You should notice that Map<Integer, something> creates Integer object for each record. Integer is not the same as int, it takes far more memory. If you have small collection, thats fine, why would we care. If you have big collection like all items, thats big deal.

Also in getMapById(int) int is cast to Integer, it takes additional time, but thats fine. You might use .getOrDefault in there, takes just 1 line.

 

ArrayList thing in MapData i skip.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...