I would like to clear all items out of an NSTreeController that I added using Controller.AddObject. Is this possible? I presume it is possible to retrieve all the index paths, sort by depth in reverse and remove the items one by one, but I'm hoping for a way just to clear the data.
Simply replace the whole tree model by calling setContent: on your NSTreeController.
This removes everything but makes adding new elements impossible:
[treeController setContent:nil];
Prefer to replace it with an empty collection instead:
[treeController setContent:#[]];
Depending on your model, you might need to do additional set-up for the collection.
Related
The MutableMap.keys property is defined as : abstract val keys: MutableSet<K>
I understand that the content of keys will change as the underlying map will change, but how can the keys it-self be modifiable ? IE : I see no logic in calling map.keys.add(xxx)
Rq: I came into this problem while creating a proxy around a MutableMap. I have to temper the entries and keys content, but do not want to implement the remove/add/clear methods
The MutableSet returned by keys throws UnsupportedOperationException if you try to add something. It provides remove and filtering (retainAll) operations, which can simplify actions that don't need to consider the values, only the keys.
If you're already using a MutableMap, it makes sense that you should also be able to work with the keys directly in a mutable way.
It corresponds to the Java Map#keySet() method which is documented as follows:
Returns a Set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation), the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.
The bolded parts explain why it's represented as MutableSet in Kotlin; otherwise you couldn't port Java code using these capabilities.
I would like to get some thoughts from others about the following problem.
Let's assume we have two classes Products and Items. Products object allows us to access any Item object. Here's the example.
$products = new Products();
// get existing item from products
$item = $products->get(123);
// create item
$item = $products->create();
$item->setName("Some new product");
$item->setPrice(2.50);
Now what would be the best way to update/save state of the item? I see 2 options:
$item->save();
or
$products->save($item);
First aproach seems very straigh forward. Once attributes of Item object are set calling save method on it will persist changes.
On the other hand I feel like latter approach is better. We're separating the roles of two objects. Item object contains only state and Products object operates on that state. This solution may be also better for writing unit tests.
Any thoughts?
So, effectively the items are buffering the actual changes.
Clearly both approaches will work, however it comes down to how closely you want to adhere to the underlying database's model or the overlaid object model.
Viewed from the outside, $item->save() makes the most sense in terms of the model - as you point out, you update the item's properties and then save them down. Plus it is conceptually an action that is performed on the item.
However, $products->save($item) offers two noticable advantages, and a drawback.
On the plus side, by moving save into products, it can (potentially) handle batching / reordering of the updates in a smarter way since it has visibility of all the items. It also allows the save code to be used as ->add() (more or less)
A downside is it is going to (from the object model view) add the following possible use, which you probably don't want:
$p1 = new Products();
$p2 = new Products();
$item = $p1->create();
// set $item values
$p2->save($item);
Obviously, you could just add an 'is this mine? no? then throw an error' test to Products::save, but that is extra code for blocking a use case the syntax implies could/should work. Or at least would probably slip through a code review.
So, I'd say go with the approach that seems the simplest and binds tightest to the desired functionality ($item->save()), unless you need to do caching/batching/whatever that forces you to go with the other.
How can we check whether the given object is a list or other type
in velocity. In that list i have another list which i need to iterate again.
I also have another data in the parent list which i want to print while iterating parent list. But the problem is the child list object also get printing with actual data. So i want to print the data by checking whether its list or not. Any help is much appreciated.
Before you get any remarks on using too much logic in templates, try this reflection based approach :
velocity (test instanceof)
I'm trying figure out if this is possible. Basically, I need to create an XElement and then add one or more siblings to that XElement, but without a parent. I'll add that list of XElements to a Parent later, but need some flexibility in building this and other lists of XElements before doing so. Is this possible?
So I would have something like:
Public items As XElement = <ItemA>Something</ItemA>
And then I need to add an element so that the result looks like:
<ItemA>Something</ItemA>
<ItemB>Something Else</ItemB>
That result is what I need to pass around as a single object. I've messed arounnd with IEnumerable(Of XElement), but there is no .Add.
Any thoughts?
then add one or more siblings to that XElement, but without a parent
That's not going to work, elements are only siblings because they share a Parent ...
You'll have to use a temporary parent that you later change or replace.
You can of course use any collection (List<XElement>) to keep a list that you later turn into a list of siblings. Not clear what your problem with that is.
I have a Dictionary of objects I have created in smalltalk, which I am iterating over by enumerating it based on the key/value pairs.
For value object in the dictionary, I am calling a method on that object. Based on certain conditions, I would like for this object to be able to add a new member to dictionary, and possibly delete another one.
I've been looking at the 'Perform' and 'Messages' facilities in Smalltalk, but I'm not sure if it is even possible to do what I'm trying to do - is it possible to return a message (or multiple messages), which another object can process and perform?
For example, could my method return 'removeKey: 19' and 'add object' at the same time?
I am using GNU Smalltalk, if it matters.
When you iterate over the collection, pass the collection as part of the argument:
aCollection copy do: [:each | each doSomethingOn: aCollection]
The copy ensures that the #doSomethingOn: can alter the original collection without messing up the iteration.
A Smalltalk method can't return multiple values, but it can return a Collection containing those values:
foo
^ Array with: 1 with: 2.
So you return a Collection with multiple methods, and just iterate over it, sending the messages in the Collection.
The class Message can do what you want:
(Message selector: #raisedTo: argument: 2) sendTo: 3
That produces "9" when evaluated.
Note, adding or removing things from a collection while iterating over it generally is not a good idea. Try copying the collection first, iterating over the copy and modifying the original from within the block being used to iterate over the copy.
As you iterate through, add the items you want to add to aDictionary, to aTemporaryDictionaryOfAdds, and the items you want to delete to aTemporaryDictionaryOfDeletes
Then iterate through each of those, adding to and removing from aDictionary as you do.
If you add the deletes to aDictionaryOfThingsDeletedFromADictionary, you have a history, too.