ActionScript: How to sort a class vector based on int - class

I have a vector of TestClass objects that I would like to arrange in ascending order based on integer of each TestClass object. How would I do this?
Main Method:
public class testSave extends Sprite
{
public function testSave()
{
var testVector:Vector.<TestClass> = new Vector.<TestClass>;
testVector.push(new TestClass(5, "Testing"), new TestClass(2, "HelloWorld"), new TestClass(7, "Ohai");
}
}
TestClass
public class TestClass
{
public function TestClass(testi:int, tests:String)
{
this.stest = tests;
this.itest = testi
}
public var stest:String;
public var itest:int;
}

Unfortunately there is no sortOn for Vectors but there is a sort. So if you do this:
public function testSave():void {
var testVector:Vector.<TestClass> = new Vector.<TestClass>;
testVector.push(new TestClass(5, "Testing"), new TestClass(2, "HelloWorld"), new TestClass(7, "Ohai"));
testVector.sort(function(a:TestClass, b:TestClass):Boolean {
return a.itest > b.itest;
});
// confirm the vector is now sorted
for (var i = 0; i < testVector.length; i++){
trace(testVector[i].itest);
}
}
your vector will sort. Vector.sort takes a sorting function, it compares the two values and then figures out how to swap them when iterating through the array...
Checking if a is greater than b will result in ascending order.

Related

Cannot convert Datatypes from-to

I have a class called DisplayInventory
Dictionary<InventoryObject, GameObject> itemsDisplayed = new
Dictionary<InventoryObject, GameObject>();
itemsDisplayed.Add(inventory.Container[i], obj);
the code breaks at this line (inventory.Container[i]) because it
cannot convert (field) List InventoryObject.Container.
this is my InventoryObject class
public class InventoryObject : ScriptableObject {
public List Container = new List();
public void AddItem(ItemObjectData _item, int _amount)
{
bool hasItem = false;
for (int i = 0; i < Container.Count; i++)
{
if(Container[i].item == _item)
{
Container[i].AddAmount(_amount);
hasItem = true;
break;
}
}
if(!hasItem)
{
Container.Add(new InventorySlot(_item, _amount));
}
}
}
[System.Serializable] public class InventorySlot {
public ItemObjectData item;
public int amount;
public InventorySlot(ItemObjectData _item, int _amount)
{
item = _item;
amount = _amount;
}
public void AddAmount(int value)
{
amount += value;
} }
The part where you declare the inventory variable or specifically inventory.container is missing.
Or is this the container from the InventoryObject class?
You need the specific type InventoryObject for the Dictionary.
What you are giving into it is simply just a List object that you put inside the container.
If you are refering to the container from the InventoryObject class, it really is just a List() that you hold there.
In this case the Dictionary would only need inventory as input.
Or you could change from
public List Container = new List();
to this
public List<InventoryObject> Container = new List<InventoryObject>();
This would still make more sense if this container is outside of the InventoryObject and in an Inventory class or something.

Copying ScriptableObjects

Is there a way of mimicking MonoBehaviour copy semantics in ScriptableObjects?
Say I have a MonoBehaviour like so:
public class DummyClassBehaviour : MonoBehaviour {
public DummyClass DummyClassTest; //ScriptableObject
public DummyClassBehaviour DummyBehaviourTest; //Another DummyClassBehaviour
}
And a ScriptableObject:
public class DummyClass : ScriptableObject {
public string Text = "";
}
When I duplicate(CTRL+D) a GameObject w/ DummyClassBehaviour attached, 'DummyBehaviourTest' copies as you would expect: If it references a MonoBehaviour in the GameObject I'm copying, the copy mechanism updates the reference to the same MonoBehaviour type in the new GameObject. If it references a MonoBehaviour in another GameObject, that reference remains unchanged.
The ScriptableObject, on the other hand, always references the original. So I end up with N GameObject's all sharing the same ScriptableObject (DummyClass) from the original GameObject. I'm using ScriptableObjects to allow serialization of non-Monobehaviour data classes.
As far as I can tell, and please someone correct me if I'm wrong, you cannot modify the serialization behavior of a ScriptableObject to match that of a MonoBehaviour. Namely that it should update references if a duplicate is made.
Instead I opted for a less than optimal solution, but it works. My class is assigned a unique identifier that gets serialized like everything else. I use this ID in DummyBehaviour.Awake() to create a lookup table that I can then use to reassign my DummyClass.
I'm not going to accept my own answer because I don't feel it answers my original question fully, but it's related:
[System.Serializable]
public class DummyClass {
// Unique id is assigned by DummyBehaviour and is unique to the game object
// that DummyBehaviour is attached to.
public int UniqueID = -1;
public string Text = "";
// Override GetHashCode so Dictionary lookups
public override int GetHashCode(){
int hash = 17;
hash = hash * 31 + UniqueID;
return hash;
}
// override equality function, allows dictionary to do comparisons.
public override bool Equals(object obj)
{
if (object.ReferenceEquals(obj, null))return false;
DummyClass item = obj as DummyClass;
return item.UniqueID == this.UniqueID;
}
// Allow checks of the form 'if(dummyClass)'
public static implicit operator bool(DummyClass a)
{
if (object.ReferenceEquals(a, null)) return false;
return (a.UniqueID==-1)?false:true;
}
public static bool operator ==(DummyClass a, DummyClass b)
{
if (object.ReferenceEquals(a, null))
{
return object.ReferenceEquals(b, null);
}
return a.Equals(b);
}
public static bool operator !=(DummyClass a, DummyClass b)
{
if (object.ReferenceEquals(a, null))
{
return object.ReferenceEquals(b, null);
}
return !a.Equals(b);
}
}
And my MonoBehaviour:
[ExecuteInEditMode]
public class DummyBehaviour : MonoBehaviour {
public List<DummyClass> DummyClasses = new List<DummyClass>();
// reassign references based on uniqueid.
void Awake(){
Dictionary<DummyClass,DummyClass> dmap = new Dictionary<DummyClass,DummyClass>();
// iterate over all dummyclasses, reassign references.
for(int i = 0; i < DummyClasses.Count; i++){
DummyClass2 d = DummyClasses[i];
if(dmap.ContainsKey(d)){
DummyClasses[i] = dmap[d];
} else {
dmap[d] = d;
}
}
DummyClasses[0].Text = "All items same";
}
// helper function, for inspector contextmenu, to add more classes from Editor
[ContextMenu ("AddDummy")]
void AddDummy(){
if(DummyClasses.Count==0)DummyClasses.Add(new DummyClass{UniqueID = 1});
else {
// Every item after 0 points to zero, serialization will remove refs during deep copy.
DummyClasses.Add(DummyClasses[0]);
}
UnityEditor.EditorUtility.SetDirty(this);
}
}

Java compiler warning for changing a method argument variable in method body

public class InsertionSort {
public static <T extends Comparable<T>> void sort(T[] array) {
for (int indexOfNextToInsert = 1; indexOfNextToInsert < array.length; indexOfNextToInsert++) {
// array from array[0] to array[indexOfNextItemToReposition - 1] is sorted
// now insert array item at "indexOfNextItemToReposition" into
// the sorted left side of array
insert(array, indexOfNextToInsert);
}
}
private static <T extends Comparable<T>> void insert(T[] array, int indexOfNextToInsert) {
T nextValue = array[indexOfNextToInsert];
while (indexOfNextToInsert > 0 && nextValue.compareTo(array[indexOfNextToInsert - 1]) < 0) {
array[indexOfNextToInsert] = array[indexOfNextToInsert - 1];
indexOfNextToInsert--; //<-- I am getting an warning here in eclipse
}
array[indexOfNextToInsert] = nextValue;
}
}
Does anyone know how to fix this warning?
You shouldn't reassign method parameters. Assignment to a parameter could be confused with an attempt to use it as an output parameter. Further discussion: http://sourcemaking.com/refactoring/remove-assignments-to-parameters.

How to Display Wicket DataTable or List View row items for an Object which has a List of Objects as a field

AID BID count1 count2
AID1 1 3 4
2 4 5
3 4 2
AID2 4 6 10
5 2 4
6 4 6
AID3 7 4 5
8 9 4
THis is the table I am trying to display that is for every AID in ObjA display a list of ObjB s.
I want to display a Sortable data using Wicket DataTable or Listview .I have used the SortableData Provider and it works for simple Objects like
ObjA{
int a;
String b;
}
But I have an Object which has List of Objects within it as field as shown below.
ObjA{
Long aId;
List<ObjB> listObjB;
}
#magomi
Thank you for your response.This is the code and the corresponding Html files. I am using DataTables.
I am not sure how to render the items in the listObjBs (bID,count,count2) in the table for every aID.
What is the right way to add the columns for the listObjBs in the ObjA
***columns.add(new PropertyColumn<ObjA>(Model.of("B ID"), "ObjBCounts.bID",
"ObjBCounts.BID"));
columns.add(new PropertyColumn<ObjA>(Model.of(" Count"), "objBCounts.count"));
columns.add(new PropertyColumn<ObjA>(Model.of("Count2”),"objBCounts.count2"));***
How do I sort on the column bID in the iterator method?
Public class HomePage extends Webpage
private static List<ObjB> listObjBs = Arrays.asList(
new ObjB(15L,2,5),
new ObjB(12L,7,10),
new ObjB(13L,3,5),
new ObjB(10L,6,5));
private static List<ObjA> contacts = Arrays.asList(
new ObjA(1L,listObjBs),
new ObjA(2L,listObjBs),
new ObjA(3L,listObjBs),
new ObjA(4L,listObjBs));
public HomePage(final PageParameters parameters) {
List<IColumn<ObjA>> columns = new ArrayList<IColumn<ObjA>>();
columns.add(new PropertyColumn<ObjA>(Model.of("A ID"), "aID",
"aID"));
Public class HomePage extends Webpage
private static List<ObjB> listObjBs = Arrays.asList(
new ObjB(15L,2,5),
new ObjB(12L,7,10),
new ObjB(13L,3,5),
new ObjB(10L,6,5));
private static List<ObjA> contacts = Arrays.asList(
new ObjA(1L,listObjBs),
new ObjA(2L,listObjBs),
new ObjA(3L,listObjBs),
new ObjA(4L,listObjBs));
public HomePage(final PageParameters parameters) {
List<IColumn<ObjA>> columns = new ArrayList<IColumn<ObjA>>();
columns.add(new PropertyColumn<ObjA>(Model.of("A ID"), "aID",
"aID"));
columns.add(new PropertyColumn<ObjA>(Model.of("B ID"), "ObjBCounts.bID",
"ObjBCounts.BID"));
columns.add(new PropertyColumn<ObjA>(Model.of(" Count"), "objBCounts.count"));
columns.add(new PropertyColumn<ObjA>(Model.of("Count2”),"objBCounts.count2"));
add(new DefaultDataTable<ObjA>("contacts", columns,
new ContactsProvider(), 10));
}
rivate static class ContactsProvider extends SortableDataProvider<ObjA> {
public ContactsProvider() {
setSort("aID", SortOrder.ASCENDING);
}
public Iterator<? extends ObjA> iterator(int first, int count) {
List<ObjA> data = new ArrayList<ObjA>(contacts);
Collections.sort(data, new Comparator<ObjA>() {
public int compare(ObjA o1, ObjA o2) {
int dir = getSort().isAscending() ? 1 : -1;
if ("bID".equals(getSort().getProperty())) {
return dir * (o1.listObjB.get(0).getBID().compareTo(o2.listObjB.get(0).getBID()));
} else {
return dir * (o1.getAID().compareTo(o2.getAID()));
}
}
});
return data.subList(first, Math.min(first + count, data.size()))
.iterator();
}
public int size() {
return contacts.size();
}
public IModel<ObjA> model(ObjA object) {
return Model.of(object);
}
}
<html>
<body>
<table border ="1" cellspacing = "1" wicket:id="contacts" class="contacts"></table>
</body>
</html>
add(new DefaultDataTable<ObjA>("contacts", columns,
new ContactsProvider(), 10));
}
rivate static class ContactsProvider extends SortableDataProvider<ObjA> {
public ContactsProvider() {
setSort("aID", SortOrder.ASCENDING);
}
public Iterator<? extends ObjA> iterator(int first, int count) {
List<ObjA> data = new ArrayList<ObjA>(contacts);
Collections.sort(data, new Comparator<ObjA>() {
public int compare(ObjA o1, ObjA o2) {
int dir = getSort().isAscending() ? 1 : -1;
if ("bID".equals(getSort().getProperty())) {
return dir * (o1.listObjB.get(0).getBID().compareTo(o2.listObjB.get(0).getBID()));
} else {
return dir * (o1.getAID().compareTo(o2.getAID()));
}
}
});
return data.subList(first, Math.min(first + count, data.size()))
.iterator();
}
public int size() {
return contacts.size();
}
public IModel<ObjA> model(ObjA object) {
return Model.of(object);
}
}
<html>
<body>
<table border ="1" cellspacing = "1" wicket:id="contacts" class="contacts"></table>
</body>
</html>
ObjB{
Long bId;
int count;
int count2;
}
It want to display data from ObjA which contains a list of objBs in a Sortable table
Could some one please help me with the wicket markup and and how to populate the columns.
Thank you.
I'm not quite sure but it looks like you are mixing ObjA and ObjB into one table in a way that is not possible. Is it right, you want to show a table of ObjA (at least this is indicated by your first column)? Or do you want to show a table of ObjB (as indicated by the second and third column)?
As long as a table is backed by one list you cannot mix another list into this table.
From my sight you have the following options:
You can can have a table of ObjA and inside a table cell you can have a panel with a table of ObjB. But there is no sorting over all rows of ObjB of all ObjA.
You can define some kind of DTO that has a object mixed from ObjA and ObjB. Something like this:
class Data() {
int objAId;
int objBId;
int count1;
int count2;
...
}
You can use a standard repeating view and implement the sorting into your own data provider.

How to assign/opt from multiple delegates for a 'moled' method?

I am currently examining Moles from the outside while I wait for my VS 2010 license, and I wonder whether Moles allows me to:
provide the ability to assígn multiple mole delegates for a method being moled, perhaps at a test fixture setup level?
switch in runtime in my test case, which of my mole delegates must be invoked for the upcoming call(s) to the moled method being isolated?
Any hints?
Best Answer:
It is much easier and makes far more sense to include gating logic in the detour method, than using two stubs for the same method! For example, MyMethod reads data from three different files on disk, each requiring different mock data to be returned. We may detour System.IO.File.OpenRead and gate the return value by analyzing the input parameters of OpenRead:
TEST METHOD:
[TestMethod]
[HostType("Moles")]
public void Test()
{
System.IO.Moles.MFile.OpenReadString = filePath => {
var mockStream = new System.IO.FileStream();
byte[] buffer;
switch (filePath)
{
case #"C:\DataFile.dat":
mockStream.Write(buffer, 0, 0); // Populate stream
break;
case #"C:\TextFile.txt":
mockStream.Write(buffer, 0, 0); // Populate stream
break;
case #"C:\LogFile.log":
mockStream.Write(buffer, 0, 0); // Populate stream
break;
}
return mockStream;
};
var target = new MyClass();
target.MyMethod();
}
TARGET TYPE:
using System.IO;
public class MyClass
{
public void MyMethod()
{
var fileAData = File.OpenRead(#"C:\DataFile.dat");
var fileBData = File.OpenRead(#"C:\TextFile.txt");
var fileCData = File.OpenRead(#"C:\LogFile.log");
}
}
Direct Answer to Your Questions:
Yes to #1: instantiate one type for each detour, and then use each for the desired behavior. And, yes to #2: act upon one instance of the mole type or the other. This requires addition of method input parameters or class constructor injection.
For example, MyMethod reads three data files from disk, and you need to pass back three different data mocks. MyMethod requires three parameters, an overtly intrusive solution. (Note input parameters are FileInfo type; because, System.IO>File is static and can not be instantiated: For example:
TEST METHOD:
[TestMethod]
[HostType("Moles")]
public void Test()
{
var fileInfoMoleA = new System.IO.Moles.MFileInfo();
fileInfoMoleA.OpenRead = () => { return new FileStream(); };
var fileInfoMoleB = new System.IO.Moles.MFileInfo();
fileInfoMoleB.OpenRead = () => { return new FileStream(); };
var fileInfoMoleC = new System.IO.Moles.MFileInfo();
fileInfoMoleC.OpenRead = () => { return new FileStream(); };
var target = new MyClass();
target.MyMethod(fileInfoMoleA, fileInfoMoleB, fileInfoMoleC);
}
TARGET TYPE:
using System.IO;
public class MyClass
{
// Input parameters are FileInfo type; because, System.IO.File
// is a static class, and can not be instantiated.
public void MyMethod(FileInfo fileInfoA, FileInfo fileInfoB, FileInfo fileInfoC)
{
var fileAData = fileInfoA.OpenRead();
var fileBData = fileInfoB.OpenRead();
var fileCData = fileInfoC.OpenRead();
}
}
UPDATE:
In response to #Chai comment, it is possible to create common methods, within the test project, that may be referenced as the mole detour delegate. For example, you may wish to write a common method that may be referenced by any unit test, that sets up a variety of pre-configured scenarios. The following example displays how a parameterized method could be used. Get creative -- they're just method calls!
TARGET TYPES:
namespace PexMoleDemo
{
public class MyClass
{
private MyMath _math;
public MyClass()
{
_math = new MyMath() { left = 1m, right = 2m };
}
public decimal GetResults()
{
return _math.Divide();
}
}
public class MyOtherClass
{
private MyMath _math;
public MyOtherClass()
{
_math = new MyMath() { left = 100m, right = 200m };
}
public decimal Divide()
{
return _math.Divide();
}
}
public class MyMath
{
public decimal left { get; set; }
public decimal right { get; set; }
public decimal Divide()
{
return left / right;
}
}
}
TEST METHODS:
ArrangeScenarios() sets up mole detours, by switching on the enumeration parameter. This allows the same scenarios to be erected, in a DRY manner, throughout many tests.
using System;
using Microsoft.Moles.Framework;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PexMoleDemo;
[assembly: MoledAssembly("PexMoleDemo")]
namespace TestProject1
{
[TestClass()]
public class ProgramTest
{
public enum Scenarios
{
DivideByZero,
MultiplyInsteadOfDivide
}
private void ArrangeScenario(Scenarios scenario)
{
switch (scenario)
{
case Scenarios.DivideByZero:
PexMoleDemo.Moles.MMyMath.AllInstances.rightGet =
instance => { return 0m; };
break;
case Scenarios.MultiplyInsteadOfDivide:
PexMoleDemo.Moles.MMyMath.AllInstances.Divide =
instance => { return instance.left * instance.right; };
break;
default:
throw new NotImplementedException("Invalid scenario.");
}
}
[TestMethod]
[HostType("Moles")]
[ExpectedException(typeof(DivideByZeroException))]
public void Test1()
{
ArrangeScenario(Scenarios.DivideByZero);
var target = new PexMoleDemo.MyClass();
var math = new PexMoleDemo.MyMath() { left = 1, right = 2 };
var left = math.left;
var right = math.right;
var actual = target.GetResults();
}
[TestMethod]
[HostType("Moles")]
public void Test2()
{
ArrangeScenario(Scenarios.MultiplyInsteadOfDivide);
// Perform some sort of test that determines if code breaks
// when values are multiplied instead of divided.
}
[TestMethod]
[HostType("Moles")]
[ExpectedException(typeof(DivideByZeroException))]
public void Test3()
{
ArrangeScenario(Scenarios.DivideByZero);
var target = new PexMoleDemo.MyOtherClass();
var math = new PexMoleDemo.MyMath() { left = 1, right = 2 };
var left = math.left;
var right = math.right;
var actual = target.Divide();
}
[TestMethod]
[HostType("Moles")]
public void Test4()
{
ArrangeScenario(Scenarios.MultiplyInsteadOfDivide);
// Perform some sort of test that determines if code breaks
// when values are multiplied instead of divided.
}
}
}