SageTV Community  

Go Back   SageTV Community > SageTV Development and Customizations > SageTV Studio
Forum Rules FAQs Community Downloads Today's Posts Search

Notices

SageTV Studio Discussion related to the SageTV Studio application produced by SageTV. Questions, issues, problems, suggestions, etc. relating to the Studio software application should be posted here.

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 08-03-2009, 07:06 PM
jphipps jphipps is offline
Sage Expert
 
Join Date: Aug 2006
Location: Maryland
Posts: 512
Remove a MediaFileAPI.List item with an Iterator

I am using Greg's API and in java, I am trying to remove some items from a MediaFileAPI.List using an Iterator to loop through the collection. When I try to remove an item, I get a java.lang.UnsupportedOperationException exception.

Anyone have any ideas???

Thanks,
Jeff

EDIT: From the research I have done, it appears that the MediaFileAPI.List is an AbstractList, and if no remove method is created, than that exception is thrown... Anyone have a good idea on editing the list without haveing the overhead of copying it to another array?

Last edited by jphipps; 08-03-2009 at 07:34 PM.
Reply With Quote
  #2  
Old 08-03-2009, 07:38 PM
GKusnick's Avatar
GKusnick GKusnick is offline
SageTVaholic
 
Join Date: Dec 2005
Posts: 5,083
The List types implemented by my API wrappers are not intended to be mutable; the add and remove methods are unsupported by design. If you need a mutable collection (i.e. one that supports adding and removing elements), create a new ArrayList or whatever passing my List into the constructor as the initial contents:

Code:
MediaFileAPI.List list1 = api.mediaFileAPI.GetMediaFiles();  // not mutable
List<MediaFileAPI.MediaFile> list2 = new ArrayList<MediaFileAPI.MediaFile>(list1);  // mutable
__________________
-- Greg
Reply With Quote
  #3  
Old 08-03-2009, 08:15 PM
jphipps jphipps is offline
Sage Expert
 
Join Date: Aug 2006
Location: Maryland
Posts: 512
Thanks, I appreciate your help.. it works great... I was worried about the performance impact on createing a new List, but it runs pretty fast...

Thanks again,
Jeff
Reply With Quote
  #4  
Old 08-04-2009, 06:01 AM
PLUCKYHD PLUCKYHD is offline
SageTVaholic
 
Join Date: Dec 2007
Posts: 6,257
Okay so that explains my issue with trying to draw elements from GetMediaFiles() within eclipse. But I am confused as GetMediaFiles() returns an object[] and I am not sure how to convert it in eclipse to an arraylist.

TIA
Reply With Quote
  #5  
Old 08-04-2009, 07:03 AM
jreichen's Avatar
jreichen jreichen is offline
Sage Icon
 
Join Date: Jul 2004
Posts: 1,192
This page has examples of converting an array to a list and vice-versa.

http://snippets.dzone.com/posts/show/343
__________________
Server: Intel Core i5 760 Quad, Gigabyte GA-H57M-USB3, 4GB RAM, Gigabyte GeForce 210, 120GB SSD (OS), 1TB SATA, HD HomeRun.
Extender: STP-HD300, Harmony 550 Remote,
Netgear MCA1001 Ethernet over Coax.
SageTV: SageTV Server 7.1.8 on Ubuntu Linux 11.04, SageTV Placeshifter for Mac 6.6.2, SageTV Client 7.0.15 for Windows, Linux Placeshifter 7.1.8 on Server and Client
, Java 1.6.
Plugins: Jetty, Nielm's Web Server, Mobile Web Interface.

Reply With Quote
  #6  
Old 08-04-2009, 07:47 AM
PLUCKYHD PLUCKYHD is offline
SageTVaholic
 
Join Date: Dec 2007
Posts: 6,257
Thanks for the example and sorry for the denseness but I am missing something and I guess having a hard time grasping arrays in eclipse
here is my code it tells me Videos[] cannot be resolved.

Code:
  Videos[] arrayofvideos = MediaFileAPI.GetMediaFiles("VDB").toArray(new Videos[]{});
Reply With Quote
  #7  
Old 08-04-2009, 08:24 AM
broconne broconne is offline
Sage Aficionado
 
Join Date: Feb 2009
Location: Cary, NC
Posts: 306
Quote:
Originally Posted by PLUCKYHD View Post
Thanks for the example and sorry for the denseness but I am missing something and I guess having a hard time grasping arrays in eclipse
here is my code it tells me Videos[] cannot be resolved.

Code:
  Videos[] arrayofvideos = MediaFileAPI.GetMediaFiles("VDB").toArray(new Videos[]{});
PluckyHD -
From this example, it looks like you are trying to convert it into an Array - but up above you said you wanted an "arraylist" which is a different construct - a list interface backed by an Array.

If you want it as an ArrayList instead of an Array you would do -
List videos = new ArrayList(MediaFileAPI.GetMediaFiles("VDB"));

as for an Array, that method for conversion looks correct. I have never used studio or this API before.. but are you use the class name is "Videos" and not "Video"? In other words does
Code:
Video[] arrayofvideos = MediaFileAPI.GetMediaFiles("VDB").toArray(new Video[]{});
work?
Reply With Quote
  #8  
Old 08-04-2009, 09:47 AM
broconne broconne is offline
Sage Aficionado
 
Join Date: Feb 2009
Location: Cary, NC
Posts: 306
After taking a quick look at the both the Sage API and Greg's API I can't find any reference to a Video or Videos class.

Is this what you want?
Code:
MediaFileAPI.MediaFile[] videos = MediaFileAPI.GetMediaFiles("VDB").toArray(new MediaFileAPI.MediaFile[]{});
Reply With Quote
  #9  
Old 08-04-2009, 09:51 AM
PLUCKYHD PLUCKYHD is offline
SageTVaholic
 
Join Date: Dec 2007
Posts: 6,257
That makes more sense thank you I am trying to learn but sometimes teaching myself it hard
Reply With Quote
  #10  
Old 08-04-2009, 11:20 AM
jphipps jphipps is offline
Sage Expert
 
Join Date: Aug 2006
Location: Maryland
Posts: 512
Greg's example above shows how to build a List from the MediaFileAPI.List that is generated from the GetMediaFiles() call.

I found using the List the best route, and you can use an Iterator or index to spin through it, and also remove items if needed...

Thanks,
Jeff
Reply With Quote
  #11  
Old 08-04-2009, 11:46 AM
GKusnick's Avatar
GKusnick GKusnick is offline
SageTVaholic
 
Join Date: Dec 2005
Posts: 5,083
The native GetMediaFiles() API method returns an Array. My wrapper function returns a List, as indicated in the documentation for my package. (Eclipse should also be able to show you the javadocs for the methods you're calling.) Result types matter; you can't assign an Array object to a List variable, and vice versa. Know what data types you're dealing with, and declare your variables accordingly.

If this is your first venture into actual Java programming (as distinct from Studio coding), I strongly recommend a good Java tutorial. Here's one place to start:

Learning the Java Language
__________________
-- Greg
Reply With Quote
  #12  
Old 08-04-2009, 12:51 PM
tmiranda's Avatar
tmiranda tmiranda is offline
SageTVaholic
 
Join Date: Jul 2005
Location: Central Florida, USA
Posts: 5,851
Greg,

Whats's the difference between a List and an Array?

Tom
__________________

Sage Server: 8th gen Intel based system w/32GB RAM running Ubuntu Linux, HDHomeRun Prime with cable card for recording. Runs headless. Accessed via RD when necessary. Four HD-300 Extenders.
Reply With Quote
  #13  
Old 08-04-2009, 12:51 PM
broconne broconne is offline
Sage Aficionado
 
Join Date: Feb 2009
Location: Cary, NC
Posts: 306
Quote:
Originally Posted by jphipps View Post
Greg's example above shows how to build a List from the MediaFileAPI.List that is generated from the GetMediaFiles() call.

I found using the List the best route, and you can use an Iterator or index to spin through it, and also remove items if needed...

Thanks,
Jeff
FYI: In general you should not use an index to spin through a list unless the result of
Code:
listName instanceof RandomAccess
is true. If you are just inspecting the elements, removing some elements, etc, it is always preferable to use an iterator.
Reply With Quote
  #14  
Old 08-04-2009, 01:04 PM
broconne broconne is offline
Sage Aficionado
 
Join Date: Feb 2009
Location: Cary, NC
Posts: 306
Quote:
Originally Posted by tmiranda View Post
Greg,

Whats's the difference between a List and an Array?

Tom
To fully answer this would require a lot of space. Because the differences are fairly technical and you would have to read the JLS (Java Language Specification) to fully understand how they are treated differently by a JVM, etc.

Practically, it is easier to list the differences that apply to most programing situations.

Arrays:
Guaranteed constant time access and updating a specific element. In other words the size of the array will not impact how long it takes to get to a certain element. (For simplification sake we are not talking about really large array that may have parts swapped out by the OS).
Elements may be accessed without method invocation. For example int x = intArray[5] would get you the 6th element of the array - arrays start at 0.
You cannot grow an array.
Arrays can hold primitive data types (ints,doubles,chars).

Lists:
Lists are an interface inside the collections package that are part of java.util
Several classes in this package implement this interface and are optimized for various tasks (An array based list, a linked based list, etc).
Lists MAY grow (It depends on the backing class)
Lists can only hold Objects, ints, chars, etc must be placed in an object before being placed in a list.


In this example, the api author has created a Class called List which seems to be a partial reimplementation of an ArrayList.

Last edited by broconne; 08-04-2009 at 01:35 PM.
Reply With Quote
  #15  
Old 08-04-2009, 01:17 PM
GKusnick's Avatar
GKusnick GKusnick is offline
SageTVaholic
 
Join Date: Dec 2005
Posts: 5,083
Quote:
Originally Posted by broconne View Post
In this example, the api author has created a Class called List which seems to be a partial reimplementation of an ArrayList.
The main reason for returning a List rather than an Array is so that I can wrap the elements lazily, on access, rather than eagerly at the time of the call. Lazy wrapping seems preferable in the case where you might access only one or two elements out of potentially thousands. My List implementation is the minimum needed to achieve this goal.

For more on Arrays v. Lists, see the Java tutorial on Collections.
__________________
-- Greg
Reply With Quote
  #16  
Old 08-04-2009, 01:57 PM
broconne broconne is offline
Sage Aficionado
 
Join Date: Feb 2009
Location: Cary, NC
Posts: 306
Quote:
Originally Posted by GKusnick View Post
The main reason for returning a List rather than an Array is so that I can wrap the elements lazily, on access, rather than eagerly at the time of the call. Lazy wrapping seems preferable in the case where you might access only one or two elements out of potentially thousands. My List implementation is the minimum needed to achieve this goal.
That makes sense. No reason to pay for it ahead of time.

Google collections as a nice way of doing the same thing now - but obviously google collections didn't exist back when this api was being written.

API looks great, I just need to find a project to use it on :-)
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Menu item disappeared sleonard SageTV Customizations 2 02-27-2009 12:01 AM
Can't get new submenu item to work Jesse SageMC Custom Interface 2 12-17-2008 07:32 AM
Table With 1 Item Being Stretched/Resized cncb SageTV Studio 3 03-25-2008 12:12 PM
finding the 'selected item' thingy... xlr8shun SageTV Studio 3 03-04-2006 09:29 PM


All times are GMT -6. The time now is 02:25 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2023, vBulletin Solutions Inc.
Copyright 2003-2005 SageTV, LLC. All rights reserved.