How to create generic and reusable code with java - java

I am on new on java.
what I am trying to do is trying to create reusable generic class.
Here is the my codes.
public interface Operation {
Boolean IsConnected();
Boolean ConnectionOpen();
Boolean ConnectionClose();
}
my main class
public class MyConnectionManager extends MyWifi{
private MyWifi _wf;
public MyConnectionManager(MyWifi wf) {
// TODO Auto-generated constructor stub
_wf= wf;
}
public Boolean IsConnected() {
// TODO Auto-generated method stub
return _wf.IsConnected();
}
public Boolean ConnectionOpen() {
// TODO Auto-generated method stub
return _wf.ConnectionOpen();
}
public Boolean ConnectionClose() {
// TODO Auto-generated method stub
return _wf.ConnectionClose();
}
}
public class MyWifi implements Operation {
public Context _context =null;
#Override
public Boolean IsConnected() {
// TODO Auto-generated method stub
ConnectivityManager connManager = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWifi.isConnected()) {
return true;
}
return false;
}
but I want it to be generic and reusable,as the type should be changable .For example ,instead of MyWifi,it could be MyBlueTooth (which implement same interface) and so on.
Here is the what trying to achive.
MyWifi wf = new MyWifi();
//MyBlueTooth bl= new MyBlueTooth ();
MyConnectionManager<MyWifi> mn= new MyConnectionManager<MyWifi>(wf);
mn.IsConnected();

You mean somethin like this?
public class MyConnectionManager<E extends Operation>{
private E _wf;
public MyConnectionManager(E wf) {
// TODO Auto-generated constructor stub
_wf= wf;
}
public Boolean IsConnected() {
// TODO Auto-generated method stub
return _wf.IsConnected();
}
public Boolean ConnectionOpen() {
// TODO Auto-generated method stub
return _wf.ConnectionOpen();
}
public Boolean ConnectionClose() {
// TODO Auto-generated method stub
return _wf.ConnectionClose();
}
}
public class Starter {
public static void main(String[] args) {
MyBlueTooth bt = new MyBlueTooth();
MyWifi wf = new MyWifi();
MyConnectionManager<MyBlueTooth> test = new MyConnectionManager<MyBlueTooth>(bt);
MyConnectionManager<MyWifi> test2 = new MyConnectionManager<MyWifi>(wf);
}
}

Change your MyConnectionManager as follows:
public class MyConnectionManager<T extends Operation> implements Operation {
private T _op;
public MyConnectionManager(T op) {
// TODO Auto-generated constructor stub
_op = op;
}
public Boolean isConnected() {
// TODO Auto-generated method stub
return _op.isConnected();
}
public Boolean connectionOpen() {
// TODO Auto-generated method stub
return _op.connectionOpen();
}
public Boolean connectionClose() {
// TODO Auto-generated method stub
return _op.connectionClose();
}
public T getOperation() {
return _op;
}
}

Related

Javafx right click edit listView item and similar methods

I am building a JavaFX todo list and am not sure how to continue. The right click popup menu works fine but I am not sure how to edit/change the contents of the ListView other than just removing them.
LocalEvent e = a string somehow?
I am trying to do 4 total things in a right click popup menu in Javafx:
Done is to place a check mark next to and strikeout the item.
Nest is to create a nested list from a list item (no idea at all how).
Edit is to make the list item editable and save the chages.
Remove works :)
I have done this by adding the following to the fxml file:
<JFXListView fx:id="eventList" editable="true" layoutX="24.0" layoutY="106.0" prefHeight="354.0" prefWidth="939.0">
<contextMenu>
<ContextMenu>
<items>
<MenuItem fx:id="popUp" mnemonicParsing="false" onAction="#Done" text="Done" />
<MenuItem fx:id="popUp3" mnemonicParsing="false" onAction="#Remove" text="Remove" />
<MenuItem fx:id="popUp1" mnemonicParsing="false" onAction="#Nest" text="Nest" />
<MenuItem fx:id="popUp2" mnemonicParsing="false" onAction="#Edit" text="Edit" />
</items>
</ContextMenu>
</contextMenu></JFXListView>`
Here is my Controller.java file:
package application;
import java.net.URL;
import java.time.LocalDate;
import java.util.ResourceBundle;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXListView;
import com.jfoenix.controls.JFXTextField;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.DatePicker;
import javafx.scene.control.MenuItem;
import javafx.scene.input.MouseEvent;
public class Controller implements Initializable{
#Override
public void initialize(URL url, ResourceBundle rb) {
datePicker.setValue(LocalDate.now());
eventList.setExpanded(true);
eventList.depthProperty().set(1);
}
#FXML
private MenuItem popUp;
#FXML
private JFXTextField textBox;
#FXML
private JFXListView<LocalEvent> eventList;
ObservableList<LocalEvent> list = FXCollections.observableArrayList();
#FXML
private JFXButton AddButton;
#FXML
private DatePicker datePicker;
#FXML
void Submit(ActionEvent event) {
list.add(new LocalEvent(datePicker.getValue(), textBox.getText()));
eventList.setItems(list);
datePicker.setValue(LocalDate.now());
textBox.setText("");
}
#FXML
public void onEnter(ActionEvent event){
list.add(new LocalEvent(datePicker.getValue(), textBox.getText()));
eventList.setItems(list);
datePicker.setValue(LocalDate.now());
textBox.setText("");
}
#FXML
void Done(ActionEvent event) {
int index = eventList.getSelectionModel().getSelectedIndex();
String str = list.get(index).toString();
str = "[✓] " + str;
LocalEvent e = null; // <- how to put a string in here?
list.set(index, e);
eventList.setItems(list);
//eventList.setItems(list.set(index, element));
}
#FXML
void Remove(ActionEvent event) {
// remove selected task
list.remove(eventList.getSelectionModel().getSelectedIndex());
}
#FXML
void Nest(ActionEvent event) {
System.out.println("How the hell do I do that? lol");
// check for nested level
// create a nested list item
}
#FXML
void Edit(ActionEvent e) {
System.out.println("Edit selection");
eventList.setEditable(true);
int index = eventList.getSelectionModel().getSelectedIndex();
eventList.scrollTo(index);
eventList.layout();
eventList.edit(index);
eventList.layout();
}
}
LocalEvent is a java class file as follows:
package application;
import java.time.LocalDate;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import javafx.beans.InvalidationListener;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
public class LocalEvent implements ObservableList<LocalEvent> {
private String description;
private LocalDate date;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public LocalDate getDate() {
return date;
}
public void setDate(LocalDate date) {
this.date = date;
}
public LocalEvent(LocalDate date, String description) {
this.setDate(date);
this.setDescription(description);
}
#Override
public String toString() {
return "At " + this.getDate() + ": " + this.getDescription();
}
#Override
public int size() {
// TODO Auto-generated method stub
return 0;
}
#Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean contains(Object o) {
// TODO Auto-generated method stub
return false;
}
#Override
public Iterator<LocalEvent> iterator() {
// TODO Auto-generated method stub
return null;
}
#Override
public Object[] toArray() {
// TODO Auto-generated method stub
return null;
}
#Override
public <T> T[] toArray(T[] a) {
// TODO Auto-generated method stub
return null;
}
#Override
public boolean add(LocalEvent e) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean remove(Object o) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean containsAll(Collection<?> c) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean addAll(Collection<? extends LocalEvent> c) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean addAll(int index, Collection<? extends LocalEvent> c) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean removeAll(Collection<?> c) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean retainAll(Collection<?> c) {
// TODO Auto-generated method stub
return false;
}
#Override
public void clear() {
// TODO Auto-generated method stub
}
#Override
public LocalEvent get(int index) {
// TODO Auto-generated method stub
return null;
}
#Override
public LocalEvent set(int index, LocalEvent element) {
// TODO Auto-generated method stub
return null;
}
#Override
public void add(int index, LocalEvent element) {
// TODO Auto-generated method stub
}
#Override
public LocalEvent remove(int index) {
// TODO Auto-generated method stub
return null;
}
#Override
public int indexOf(Object o) {
// TODO Auto-generated method stub
return 0;
}
#Override
public int lastIndexOf(Object o) {
// TODO Auto-generated method stub
return 0;
}
#Override
public ListIterator<LocalEvent> listIterator() {
// TODO Auto-generated method stub
return null;
}
#Override
public ListIterator<LocalEvent> listIterator(int index) {
// TODO Auto-generated method stub
return null;
}
#Override
public List<LocalEvent> subList(int fromIndex, int toIndex) {
// TODO Auto-generated method stub
return null;
}
#Override
public void addListener(InvalidationListener listener) {
// TODO Auto-generated method stub
}
#Override
public void removeListener(InvalidationListener listener) {
// TODO Auto-generated method stub
}
#Override
public void addListener(ListChangeListener<? super LocalEvent> listener) {
// TODO Auto-generated method stub
}
#Override
public void removeListener(ListChangeListener<? super LocalEvent> listener) {
// TODO Auto-generated method stub
}
#Override
public boolean addAll(LocalEvent... elements) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean setAll(LocalEvent... elements) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean setAll(Collection<? extends LocalEvent> col) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean removeAll(LocalEvent... elements) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean retainAll(LocalEvent... elements) {
// TODO Auto-generated method stub
return false;
}
#Override
public void remove(int from, int to) {
// TODO Auto-generated method stub
}
}
Any help at all appreciated.
The first thing that stands out was mentioned in the comments: your LocalEvent shouldn't implements ObservableList, it's just a data holder. You probably will want it to also hold a boolean to see if it's completed:
public class LocalEvent {
private String description;
private LocalDate date;
private boolean completed = false;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public LocalDate getDate() {
return date;
}
public void setDate(LocalDate date) {
this.date = date;
}
public void setCompleted(boolean completed) {
this.completed= completed;
}
public LocalEvent(LocalDate date, String description) {
this.setDate(date);
this.setDescription(description);
}
#Override
public String toString() {
String base = "At " + this.getDate() + ": " + this.getDescription();
return completed ? "[✓] " + base : base;
}
}
I don't know what the jfoenix controls are, but I'll assume they are close enough to JavaFX standard controls. The ListView should be bound to the list of events:
ObservableList<LocalEvent> list = FXCollections.observableArrayList();
ListView<LocalEvent> eventList = new ListView<>(list);
(I would also rename the lists to something that makes more sense.)
Now any change to list will be reflected in eventList so you don't need to touch eventList. This allows your done method to look like:
#FXML
void done(ActionEvent event) {
int index = eventList.getSelectionModel().getSelectedIndex();
LocalEvent localEvent = list.get(index);
localEvent.setCompleted(true);
list.set(index, localEvent);
}
Note that we needed to reset the item in the list because it doesn't know that a field of the LocalEvent instance has changed. You can make it that the fields also report changes by using an extractor, see here and many other answers here about it.
Editing depends on the cell used by the ListView, I would suggest reading about it in the docs and other questions here. Note that you probably want to be able to edit the description and date separately and not the whole toString value, so you'll have to provide a mechanism for that like bringing back the DatePicker.
Nesting depends on what you want it to look like. You might need to define your own cell factory, but consider just adding an indentation, via "\t"s, which will make it looks nested. If you really need a nesting in the data model, then each LocalEvent will have to hold its own ObservableList<LocalEvent> nestedEvents.
Also, method names should start with a lowercase.

JRHtmlExporter is deprecated now. How to define the path for saving images?

The JRHtmlExporter class is deprecated now (JasperReports 6.x).
I replaced the usage of this class with HtmlExporter. But I can not find equivalent function to replace exporter.setParameter (JRHtmlExporterParameter.IMAGES_URI, imageURI);. I need to set path for storing images for generated report (html file).
My old code:
JRHtmlExporter exporter = new JRHtmlExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, filedReport);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, outputFileName);
exporter.setParameter(JRHtmlExporterParameter.BETWEEN_PAGES_HTML, "");
exporter.setParameter(JRHtmlExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.TRUE);
String imageURI = "q?srvAction=ReportImage&img="+returnFileName.substring(3).replace("/", "%2F")+"_files"+"%2F";
exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI,imageURI);
What will be the actual code for JasperReports 6.x for defining path to images?
As we can see from the javadoc the JRHtmlExporterParameter.IMAGES_URI parameter is really deprecated and the HtmlExporterOutput.getImageHandler() method should be used instead of it.
Define the path where images are storing
We can use the implementation of HtmlResourceHandler interface, for example WebHtmlResourceHandler.
The example of using:
JRExporter exporter = new HtmlExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
SimpleHtmlExporterOutput output = new SimpleHtmlExporterOutput(out);
output.setImageHandler(new WebHtmlResourceHandler("/reports/image?image={0}"));
exporter.setExporterOutput(output);
exporter.exportReport();
Define where to save images during export
With help of FileHtmlResourceHandler handler we can set the path for images for generated html
The example of using:
JRExporter exporter = new HtmlExporter();
// output file for generated html report
File file = new File(String.format("./out/%1$s_%2$s.html", report.getTemplateName(), new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())));
ExporterConfiguration configuration = new SimpleHtmlExporterConfiguration();
exporter.setConfiguration(configuration);
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
SimpleHtmlExporterOutput exporterOutput = new SimpleHtmlExporterOutput(file);
// the folder for storing images. It will be subfolder with name starting like generated html and ended with postfix "_images"
File resourcesDir = new File(file.getParent(), file.getName() + "_images");
// argument ({0}) will be replaced with the real image name
String pathPattern = resourcesDir.getName() + "/{0}";
exporterOutput.setImageHandler(new FileHtmlResourceHandler(resourcesDir, pathPattern));
exporter.setExporterOutput(exporterOutput);
exporter.exportReport();
The generated files and folders will be like this:
.. [Folder]
image-test_20170504232649.html [File]
image-test_20170504232649.html_images [Folder]
img_0_0_0.png [File]
Notes:
The sample of using HtmlResourceHandler can be found here
You could try this for Jasper Report 6.9.0, this is the default method
JasperExportManager.exportReportToHtmlFile(jasperPrint, jasperPrint.getName() + ".html");
But if you want to configure image resource handler and any other configuration, try this
HtmlExporter exporter = new HtmlExporter();
File destFile = new File(jasperPrint.getName() + ".html");
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
HtmlReportConfiguration reportConfig = getHTMLReportConfig();
exporter.setConfiguration(reportConfig);
HtmlExporterConfiguration exporterConfig = getHTMLExporterConfig();
exporter.setConfiguration(exporterConfig);
SimpleHtmlExporterOutput output = new SimpleHtmlExporterOutput(destFile);
HtmlResourceHandler imageHandler = new WebHtmlResourceHandler(
request.getContextPath() + "/jasper/image?image={0}");
output.setImageHandler(imageHandler);
exporter.setExporterOutput(output);
exporter.exportReport();
And this is configuration for HTMLExporter and HTMLReport
private HtmlExporterConfiguration getHTMLExporterConfig() {
return new HtmlExporterConfiguration() {
#Override
public Boolean isOverrideHints() {
// TODO Auto-generated method stub
return null;
}
#Override
public Boolean isFlushOutput() {
// TODO Auto-generated method stub
return null;
}
#Override
public String getHtmlHeader() {
// TODO Auto-generated method stub
return null;
}
#Override
public String getHtmlFooter() {
// TODO Auto-generated method stub
return null;
}
// biar tidak ada paging (khusus html)
#Override
public String getBetweenPagesHtml() {
return "";
}
};
}
private HtmlReportConfiguration getHTMLReportConfig() {
return new HtmlReportConfiguration() {
#Override
public Boolean isOverrideHints() {
// TODO Auto-generated method stub
return null;
}
#Override
public Integer getStartPageIndex() {
// TODO Auto-generated method stub
return null;
}
#Override
public JRExportProgressMonitor getProgressMonitor() {
// TODO Auto-generated method stub
return null;
}
#Override
public Integer getPageIndex() {
// TODO Auto-generated method stub
return null;
}
#Override
public Integer getOffsetY() {
// TODO Auto-generated method stub
return null;
}
#Override
public Integer getOffsetX() {
// TODO Auto-generated method stub
return null;
}
#Override
public JRHyperlinkProducerFactory getHyperlinkProducerFactory() {
// TODO Auto-generated method stub
return null;
}
#Override
public ExporterFilter getExporterFilter() {
// TODO Auto-generated method stub
return null;
}
#Override
public Integer getEndPageIndex() {
// TODO Auto-generated method stub
return null;
}
#Override
public Boolean isWrapBreakWord() {
// TODO Auto-generated method stub
return null;
}
#Override
public Boolean isWhitePageBackground() {
// TODO Auto-generated method stub
return null;
}
#Override
public Boolean isUseBackgroundImageToAlign() {
// TODO Auto-generated method stub
return null;
}
// biar gak terlalu banyak white space
#Override
public Boolean isRemoveEmptySpaceBetweenRows() {
return true;
}
#Override
public Boolean isIgnorePageMargins() {
return true;
}
#Override
public Boolean isIgnoreHyperlink() {
// TODO Auto-generated method stub
return null;
}
#Override
public Boolean isEmbeddedSvgUseFonts() {
// TODO Auto-generated method stub
return null;
}
#Override
public Boolean isEmbedImage() {
// TODO Auto-generated method stub
return null;
}
#Override
public Boolean isConvertSvgToImage() {
// TODO Auto-generated method stub
return null;
}
#Override
public Boolean isAccessibleHtml() {
// TODO Auto-generated method stub
return null;
}
#Override
public Float getZoomRatio() {
// TODO Auto-generated method stub
return null;
}
#Override
public HtmlSizeUnitEnum getSizeUnit() {
// TODO Auto-generated method stub
return null;
}
#Override
public HtmlBorderCollapseEnum getBorderCollapseValue() {
// TODO Auto-generated method stub
return null;
}
#Override
public String getBorderCollapse() {
// TODO Auto-generated method stub
return null;
}
};
}
See this http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/export/JRHtmlExporterParameter.html

JCombobox using hasmap/ no point model

Hey i am looking for solving a problem i need to make a model for jcombobox.
I have a :
Map<Integer, Pathes_format> profiles =new HashMap<Integer, Pathes_format>();
I wana to show on jcombobox pathes_format.getname will be displayed at index pathes_format.GetiD i never wrote or touch a abstract class or models.
Here what i made
package subDialogs;
import java.util.HashMap;
import java.util.Map;
import javax.swing.ComboBoxModel;
import javax.swing.event.ListDataListener;
import json.Pathes_format;
public class PatheseModel implements ComboBoxModel {
Map<Integer, Pathes_format> profiles =new HashMap<Integer, Pathes_format>();
int index=-1;
#Override
public int getSize() {
// TODO Auto-generated method stub
return profiles.size();
}
#Override
public Object getElementAt(int index) {
// TODO Auto-generated method stub
return profiles.get(index);
}
#Override
public void addListDataListener(ListDataListener l) {
// TODO Auto-generated method stub
}
#Override
public void removeListDataListener(ListDataListener l) {
// TODO Auto-generated method stub
}
#Override
public void setSelectedItem(Object anItem) {
// TODO Auto-generated method stub
}
#Override
public Object getSelectedItem() {
// TODO Auto-generated method stub
return null;
}
//void addElement(Object obj){
//
//}
void insertElementAt(Object obj, int index) {
profiles.put(index, (Pathes_format) obj);
}
void removeElement(Object obj) {
Pathes_format tmp = profiles.get(obj);
tmp=null;
}
void removeElementAt(int index){
profiles.remove(index);
}
}
I don't know is this correct :/. Mby i should make for a format patches single not for map; and add ability to add map??

android dictionary int, Imageview

I am failing to put objects in dictionary and get them, I want to put imageViews against int but failing without any error
dictionary declared as:
Dictionary<Integer, ImageView> dictPlayerAll = new Dictionary<Integer, ImageView>(){
#Override
public int size() {
// TODO Auto-generated method stub
return 0;
}
#Override
public ImageView remove(Object key) {
// TODO Auto-generated method stub
return null;
}
#Override
public ImageView put(Integer key, ImageView value) {
// TODO Auto-generated method stub
return null;
}
#Override
public Enumeration<Integer> keys() {
// TODO Auto-generated method stub
return null;
}
#Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
#Override
public ImageView get(Object key) {
// TODO Auto-generated method stub
return null;
}
#Override
public Enumeration<ImageView> elements() {
// TODO Auto-generated method stub
return null;
}
};;
here putting values in dictionary
int mTag = iv.getTag();//its my imageview
dictPlayerAll.put(mTag, iv);
but it shows zero size
Your update makes more sense, but the Dictionary class is still obselete. From the documentation:
java.util
Class Dictionary
...
Note: Do not use this class since it is obsolete.
Since you want to create a table indexed by Integers (new Dictionary<Integer, ImageView>()) we should use a SparseArray.
Also when you use setTag() you convert your Integer to an Object and every time you use getTag() you are converting your Integer back from an Object. This works, but if you can use getId() it will be faster.
I recommend this:
SparseArray<ImageView> allPlayers = new SparseArray<ImageView>();
...
allPlayers.put(iv.getId(), iv);

Getting null value

I am using com.android.internal.telephony API's. In that I called two abstract classes they are Call.java and Connection.java. You can find these classes here http://hi-android.info/src/com/android/internal/telephony/Call.java.html and http://hi-android.info/src/com/android/internal/telephony/Connection.java.html for these created subclasses like
Call myCall = new MyCall();
Connection myConn = new MyConnection();
I need to use getDisconnectCause method from connection class which is an abstract method, I used like this:
myConn = myCall.getEarliestConnection();
if(myConn == null){
System.out.println("myConn is null ******");
}else
{
Connection.DisconnectCause cause = myConn.getDisconnectCause();
System.out.println("value of cause ******"+cause);
}
The subclass of Call.java is:
1. CallManager cm = CallManager.getInstance();
2. Phone.State state;
3.
4. public List<Connection> getConnections() {
5. state = cm.getState();
6. ringingCall = cm.getForegroundCalls();
7. System.out.println("**inside getConnections="+state);
8. System.out.println("**inside getConnections="+ringingCall);
9. if ( ringingCall == null) {
10. System.out.println("**call is null***");
11. return emptyConnections;
12. }
13. else
14. {
15. System.out.println("**call is not null***");
16. return ((Call) ringingCall).getConnections();
17. }
18. #Override
19. public Phone getPhone() {
20. return null;
}
#Override
public void hangup() throws CallStateException {
}
#Override
public boolean isMultiparty() {
return false;
}
public Connection
getEarliestConnection() {
List l;
long time = Long.MAX_VALUE;
Connection c;
Connection earliest = null;
68. l = getConnections();
if (l == null) {
return null;
}else if ( l.size() == 0)
{
return null;
}
for (int i = 0, s = l.size() ; i < s ; i++) {
c = (Connection) l.get(i);
long t;
t = c.getCreateTime();
if (t < time) {
earliest = c;
time = t;
}
}
return earliest;
}
}
AND the Connection.java subclass is:
class MyConnection extends Connection{
#Override
public void cancelPostDial() {
// TODO Auto-generated method stub
}
#Override
public String getAddress() {
// TODO Auto-generated method stub
return null;
}
#Override
public Call getCall() {
// TODO Auto-generated method stub
return null;
}
#Override
public long getConnectTime() {
// TODO Auto-generated method stub
return 0;
}
#Override
public long getCreateTime() {
// TODO Auto-generated method stub
return 0;
}
#Override
public DisconnectCause getDisconnectCause() {
// TODO Auto-generated method stub
return null;
}
#Override
public long getDisconnectTime() {
// TODO Auto-generated method stub
return 0;
}
#Override
public long getDurationMillis() {
// TODO Auto-generated method stub
return 0;
}
#Override
public long getHoldDurationMillis() {
// TODO Auto-generated method stub
return 0;
}
#Override
public int getNumberPresentation() {
// TODO Auto-generated method stub
return 0;
}
#Override
public PostDialState getPostDialState() {
// TODO Auto-generated method stub
return null;
}
#Override
public String getRemainingPostDialString() {
// TODO Auto-generated method stub
return null;
}
#Override
public UUSInfo getUUSInfo() {
// TODO Auto-generated method stub
return null;
}
#Override
public void hangup() throws CallStateException {
// TODO Auto-generated method stub
}
#Override
public boolean isIncoming() {
// TODO Auto-generated method stub
return false;
}
#Override
public void proceedAfterWaitChar() {
// TODO Auto-generated method stub
}
#Override
public void proceedAfterWildChar(String arg0) {
// TODO Auto-generated method stub
}
#Override
public void separate() throws CallStateException {
// TODO Auto-generated method stub
}
}
EDIT 2 : I have edited line number 1 to 17. Plz check this. I am getting java.lang.ClassCastException: java.util.Collections error on Line No: 16 and Line No:68. Can anybody help me to resolve this. And also I am getting only one state of call i.e IDLE always even though the call is not-null . I am getting inside else part. plz help me.
#Override
public List<Connection> getConnections() {
return null;
}
This method on your MyCall class returns null and in your code for MyCall.getEarliestConnection(); it returns null if getConnections() returns null.
Your implementation of the getConnections() method has it returning null.
As you can see from the code inside getEarliestConnection(), if getConnections() returns null, then getEarliestConnection() will also return null.
Here is the relevant code:
#Override
public List<Connection> getConnections() {
return null;
}
...
l = getConnections();
if (l == null) {
return null;
}
I don't know what this code is supposed to do, but it's clear why that is null at that point.
In "MyCall.getEarliestConnection()", it calls it's own "getConnections()" method, which just returns null.

Categories

Resources