I'm trying to implement a scala function object (specifically - to use with apache spark)
the equivalent java type is scala.Function0 (for a parameter less function)
besides the method that implements the business logic apply(), I see that I have to implement several other methods.
Simple googling the method names didn't help (I saw reference regarding a tag method which should be implemented - but the method name here are different.
Here is the code with those methods (with empty implementation).
private static class functionObject implements Function0<Integer>{
#Override
public Integer apply() {
// TODO Auto-generated method stub
return null;
}
#Override
public byte apply$mcB$sp() {
// TODO Auto-generated method stub
return 0;
}
#Override
public char apply$mcC$sp() {
// TODO Auto-generated method stub
return 0;
}
#Override
public double apply$mcD$sp() {
// TODO Auto-generated method stub
return 0;
}
#Override
public float apply$mcF$sp() {
// TODO Auto-generated method stub
return 0;
}
#Override
public int apply$mcI$sp() {
// TODO Auto-generated method stub
return 0;
}
#Override
public long apply$mcJ$sp() {
// TODO Auto-generated method stub
return 0;
}
#Override
public short apply$mcS$sp() {
// TODO Auto-generated method stub
return 0;
}
#Override
public void apply$mcV$sp() {
// TODO Auto-generated method stub
}
#Override
public boolean apply$mcZ$sp() {
// TODO Auto-generated method stub
return false;
}
}
What are these methods? how are they supposed to be implemented? is there a cleaner solution that adding these to any class?
Scalas FunctionX interfaces are traits, whose type parameters are specialized. Because specialization is done by scalac and therefore can't be done by javac you have to implement everything related to specialization by yourself.
Scala provides the abstract classes AbstractFunctionX, which can be easily implemented on the Java side:
// example Int => Int function
class Z extends scala.runtime.AbstractFunction1<Integer, Integer> {
public Integer apply(Integer i) {
return i;
}
}
Related
My question just very short."How to use abstract method or example used in this method?"
This method is from org.zkoss.zul.TreeModel
tmtAtasan = new TreeModel<Map<String,Object>>() {
#Override
public void addTreeDataListener(TreeDataListener arg0) {
// TODO Auto-generated method stub
}
#Override
public Map<String, Object> getChild(int[] arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public Map<String, Object> getChild(Map<String, Object> arg0,
int arg1) {
// TODO Auto-generated method stub
return null;
}
#Override
public int getChildCount(Map<String, Object> arg0) {
// TODO Auto-generated method stub
return 0;
}
#Override
public int getIndexOfChild(Map<String, Object> arg0,
Map<String, Object> arg1) {
// TODO Auto-generated method stub
return 0;
}
#Override
public int[] getPath(Map<String, Object> arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public Map<String, Object> getRoot() {
// TODO Auto-generated method stub
return null;
}
#Override
public boolean isLeaf(Map<String, Object> arg0) {
// TODO Auto-generated method stub
return false;
}
#Override
public void removeTreeDataListener(TreeDataListener arg0) {
// TODO Auto-generated method stub
}
};
I am badly stuck into this. Any help would be really appreciateable.
Thanks in advance!
Ok so from what I understand you just want to use a generic TreeModel without needing to redefine specific behaviors.
So let's imagine your model is a list of Employee beans like :
public class Employee {
private String name;
private List<Employee> listSubordinates = new ArrayList<Employee>();
public Employee(String pName) {
name = pName;
}
public void setName(String pName) {
this.name = pName;
}
public String getName() {
return name;
}
public List<Employee> getListSubordinates() {
return listSubordinates;
}
public void setListSubordinates(List<Employee> pListSubordinates) {
this.listSubordinates = pListSubordinates;
}
}
For this example, we'll imagine you have retrieved a list of employee that are sorted by hierarchy already (to simplify the example).
Employee boss1 = new Employee("Boss1");
Employee sub1 = new Employee("Sub1");
boss1.getListSubordinates().add(sub1);
Employee sub2 = new Employee("Sub2");
boss1.getListSubordinates().add(sub2);
Employee boss2 = new Employee("Boss2");
Employee sub3 = new Employee("Sub3");
boss2.getListSubordinates().add(sub3);
List<Employee> listBosses = Arrays.asList(boss1, boss2);
Again this is a simple example with just one level of hierarchy, if you had a variable level of hierarchy the following code would have to be recursive.
// Build the list of the nodes sorted by hierarchy
List<DefaultTreeNode<Employee>> firstLevelNodes = new ArrayList<DefaultTreeNode<Employee>>();
// For each employee of the highest level
for (Employee boss : listBosses) {
// Build the list of its sub employee
List<DefaultTreeNode<Employee>> listSubordinates = new ArrayList<DefaultTreeNode<Employee>>();
for (Employee subordinate : boss.getListSubordinates()) {
listSubordinates.add(new DefaultTreeNode<Employee>(subordinate));
}
// Then build the boss node with its data and its children nodes
DefaultTreeNode<Employee> bossNode = new DefaultTreeNode<Employee>(boss, listSubordinates);
// And add it to the list of first level nodes
firstLevelNodes.add(bossNode);
}
// Build the ROOT, a 'technical' node containing the nodes of the tree.
DefaultTreeNode<Employee> root = new DefaultTreeNode<Employee>(null, firstLevelNodes);
// Create the TreeModel
TreeModel treeModel = new DefaultTreeModel<Employee>(root);
And now you just have to set the TreeModel to your Tree component.
Hope this helps.
In my project I have a TableViewer which has a model, a content provider and a label provider.
Once I update my model I call tableviewer.refresh(true) and according to the documentation here:
TableViewer Methods
I'm expecting my selections to be preserved.
Unfortunately this doesn't happen. Does anybody know a solution for that?
Is it something that I miss in here or is it a bug?
EDIT:
This is my model class(counter is for testing purposes to be sure that I have the same list returned after the first refresh):
public class ItemWorkgroup {
List<Item> currentItems = new ArrayList<Item>();
static int counter = 0;
public ItemWorkgroup()
{
}
public void add(Item item)
{
currentItems.add(item);
}
public Object[] getItems()
{
return currentItems.toArray();
}
public void addList(List<Item> newItemsList)
{
System.out.println("Current items first 1: "+currentItems);
if(counter == 0)
{
currentItems = newItemsList;
counter++;
}
System.out.println("Current items first 2: "+currentItems);
}
public List<Item> getItemList()
{
return currentItems;
}
}
This is the content provider class:
public class ContentProvider implements IStructuredContentProvider{
private Mediator mediator;
private ItemWorkgroup model;
public ContentProvider(Mediator mediator, ItemWorkgroup model) {
// TODO Auto-generated constructor stub
this.mediator = mediator;
this.model = model;
}
#Override
public void dispose() {
// TODO Auto-generated method stub
}
#Override
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
// TODO Auto-generated method stub
System.out.println("Input changed");
}
#Override
public Object[] getElements(Object inputElement) {
// TODO Auto-generated method stub
System.out.println("Getting elements");
if(inputElement instanceof ItemWorkgroup)
{
return ((ItemWorkgroup) inputElement).getItems();
}
else
return new Object[0];
}
public ItemWorkgroup getItems()
{
//Items is a list of items that I'm getting from somewhere
model.addList(items);
return model;
}
The viewer tries to maintain the selection but if your content provider returns different objects for the elements that were selected after the refresh then the tree viewer will not be able to restore the selection.
Your content provider must return the same object for things which have not changed for the selection to be preserved. (Or you can return an object where the equals and hashCode methods make it appear the same).
I had some difficulties in sorting in decreasing order the elements of the following abstract class and its extensions.
package BankServices;
public abstract class Operation {
public Operation (int date, double value){
}
public abstract double getValue();
public abstract int getDate();
public abstract String toString();
}
package BankServices;
public class Deposit extends Operation {
private int date;
private int value;
public Deposit(int date, double value) {
super(date, value);
// TODO Auto-generated constructor stub
}
#Override
public String toString() {
// TODO Auto-generated method stub
return date + "," + value + "+";
}
#Override
public double getValue() {
// TODO Auto-generated method stub
return value;
}
#Override
public int getDate() {
// TODO Auto-generated method stub
return date;
}
}
package BankServices;
public class Withdrawal extends Operation{
private int date;
private double value;
public Withdrawal(int date, double value) {
super(date, value);
// TODO Auto-generated constructor stub
}
#Override
public String toString() {
// TODO Auto-generated method stub
return date + "," + value + "-";
}
#Override
public double getValue() {
// TODO Auto-generated method stub
return value;
}
#Override
public int getDate() {
// TODO Auto-generated method stub
return date;
}
}
I had to implement these methods of the main class returning sorted lists in descending order:
public List<Operation> getMovements() {
Collections.sort(operations, new Comparator<Operation>(){
public int compare(Operation a, Operation b){
return (int) (b.getDate() - a.getDate());
}
});
return operations;
}
public List<Deposit> getDeposits() {
Collections.sort(deposits, new Comparator<Operation>(){
public int compare(Operation a, Operation b){
return (int) (b.getValue() - a.getValue());
}
});
return deposits;
}
public List<Withdrawal> getWithdrawals() {
Collections.sort(withdrawals, new Comparator<Operation>(){
public int compare (Operation a, Operation b){
return (int) (b.getValue() - a.getValue());
}
});
return withdrawals;
}
the first one returns a List ordered by date, while getDeposits() and getWithdrawals() return List and List ordered by value..
Could you please suggest how to make it work without mistakes and failures?
Thank you very much in advance.
Instead of this - use compareTo(...) method which is both on Double and Date, so for example:
Collections.sort(withdrawals, new Comparator<Operation>(){
public int compare (Operation a, Operation b){
return Double.valueOf(b.getValue()).compareTo(Double.valueOf(a.getValue()));
}
});
Still, your code should work, except for some weird data you can have there... But not for real life data I think
EDIT: I was wrong, your code would only work if difference between doubles would be over 1.
This question already has answers here:
Type List vs type ArrayList in Java [duplicate]
(15 answers)
What is a List vs. an ArrayList? [duplicate]
(6 answers)
Closed 9 years ago.
What is the diference between using Array List and a List. Actually Java throws me an exception when I want to instantiate the List with istself, in fact I have to do it with a ArrayList.
List is an abstract type - a java interface. An interface defines what methods may be found on an object that implements List.
An ArrayList is a concrete implementation of the List interface (that uses an array internally to store the elements, hence the name).
An ArrayList is a List, but a List is not necessarily an ArrayList. There are other implementations, such as LinkedList.
See Liskov substitution principle
List is an interface, ArrayList is a class that implements List. You can't instantiate an interface.
A List is a general type of object known as an interface. There are many types of lists including but not limited to ArrayList and LinkedList. The list interface provide handles to a set of methods required for manipulating a list without knowing what type of list it is. Any type of list can be assigned to a variable of type List. But list itself cannot be instantiated because it is only an interface and not an implementation. See the Java list tutorial for more information. http://docs.oracle.com/javase/tutorial/collections/interfaces/list.html
Your code throws an exception cause List is abstract you can't instantiate something abstract.
List is an Interface. Here you define the contract the behaviour signature of this type.
ArrayList is a List , cause implements List, ArrayList is a concrete implementation so then have all methods defined by the List interface.
In Java you can apparently instantiate an interface making anonymous classes but you really have a concrete class that implements List contract with no-name (that's why the anonymous name).
Here is an example
List list = new List(){
#Override
public boolean add(Object arg0) {
// TODO Auto-generated method stub
return false;
}
#Override
public void add(int arg0, Object arg1) {
// TODO Auto-generated method stub
}
#Override
public boolean addAll(Collection arg0) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean addAll(int arg0, Collection arg1) {
// TODO Auto-generated method stub
return false;
}
#Override
public void clear() {
// TODO Auto-generated method stub
}
#Override
public boolean contains(Object arg0) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean containsAll(Collection arg0) {
// TODO Auto-generated method stub
return false;
}
#Override
public Object get(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public int indexOf(Object arg0) {
// TODO Auto-generated method stub
return 0;
}
#Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
#Override
public Iterator iterator() {
// TODO Auto-generated method stub
return null;
}
#Override
public int lastIndexOf(Object arg0) {
// TODO Auto-generated method stub
return 0;
}
#Override
public ListIterator listIterator() {
// TODO Auto-generated method stub
return null;
}
#Override
public ListIterator listIterator(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public boolean remove(Object arg0) {
// TODO Auto-generated method stub
return false;
}
#Override
public Object remove(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public boolean removeAll(Collection arg0) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean retainAll(Collection arg0) {
// TODO Auto-generated method stub
return false;
}
#Override
public Object set(int arg0, Object arg1) {
// TODO Auto-generated method stub
return null;
}
#Override
public int size() {
// TODO Auto-generated method stub
return 0;
}
#Override
public List subList(int arg0, int arg1) {
// TODO Auto-generated method stub
return null;
}
#Override
public Object[] toArray() {
// TODO Auto-generated method stub
return null;
}
#Override
public Object[] toArray(Object[] arg0) {
// TODO Auto-generated method stub
return null;
}
};
As #Bohemian says, if you make your own list implementation you have to take care about Liskov Substitution Principle,to don't break this and follow the contract.
I have a ContentProvider for a TreeSelectionDialog. I need to implement the getParent method in order to select the root of a tree if one of its nodes is checked. This is the code:
#SuppressWarnings("unchecked")
protected Node<T> getAdapter(Object element) {
if(element instanceof Tree)
return ((Tree<T>)element).getRootElement();
else
return (Node<T>)element;
}
#Override
public void dispose() {
// TODO Auto-generated method stub
}
#Override
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
// TODO Auto-generated method stub
}
#Override
public Object[] getElements(Object inputElement) {
return getChildren(inputElement);
}
#Override
public Object[] getChildren(Object parentElement) {
if(parentElement instanceof org.db.normalization.Table) {
if(((org.db.normalization.Table)parentElement).getStatus() == Status.DELETED)
return new Object[0];
List<org.db.normalization.Attribute> atts = new ArrayList<org.db.normalization.Attribute>();
for(Attribute a:((org.db.normalization.Table)parentElement).getAttributes().getAttributes())
if(a.getStatus() != Status.UNMODIFIED)
atts.add(a);
for(Attribute a:((org.db.normalization.Table)parentElement).getPrimaryKey().getAttributes())
if(a.getStatus() != Status.UNMODIFIED)
atts.add(a);
return atts.toArray();
} else if (parentElement instanceof org.db.normalization.Attribute) {
return new Object[0];
} else {
#SuppressWarnings("unchecked")
List<org.db.normalization.Table> n = (ArrayList<org.db.normalization.Table>)parentElement;
if (n.size() > 0) {
return n.toArray() ;
}
}
return new Object[0];
}
#Override
public Object getParent(Object element) {
// TODO Auto-generated method stub
return null;
}
#Override
public boolean hasChildren(Object element) {
// TODO Auto-generated method stub
return getChildren(element).length > 0;
}
I really have no idea of what to write in the getParent method, since I have no other information than the element received as a parameter, and this element alone, doesn't know its parent.
Thanks!
Most instances of a tree implementation, you do know your parent, so parents are either set by a setter method or on the constructor. You have no idea who the parent is, so you are presenting the worse case, where you basically have to get all node, and check rather the children of each node contain you.