10 Replies - 283 Views - Last Post: 12 October 2012 - 01:14 PM Rate Topic: -----

#1 crazedmeph  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 93
  • Joined: 19-October 09

Generic Casting Question

Posted 12 October 2012 - 07:46 AM

I am confused on how to make this more generic, I want to be able to send a File or a String to a function and then the function will either make a scanner with a string or a scanner with a file, i know i can overload but i want to make only one method and i cant seem to figure it out without sending the class name to the method too. Here is the code.

protected static ArrayList<Byte> getBytes(Object file){
		Scanner scan;
		ArrayList<Byte> bytes = new ArrayList<Byte>();
		try{
                             /*this is what i mean i know this wont work but im trying to show what i mean :P/>
                               I want to cast the Object so that file is either a string or a File
                             */
				scan = new Scanner((file.getClass())file);
                                                                                         
			
			scan.useDelimiter("\\n");
			while (scan.hasNext()) {
				String tmp = scan.next();
				for(int i = 0; i < tmp.length(); i++){
					bytes.add((byte)tmp.charAt(i));
				}
			}
			scan.close();
		}catch(Exception e){System.out.println("Exception " + e);}
		
		return bytes;
	}



Is This A Good Question/Topic? 1
  • +

Replies To: Generic Casting Question

#2 Kakerergodt  Icon User is offline

  • D.I.C Head

Reputation: 87
  • View blog
  • Posts: 201
  • Joined: 01-May 12

Re: Generic Casting Question

Posted 12 October 2012 - 07:52 AM

You could always use "instanceof",

if(file instanceof File) cast to file
else if(file instanceof String) cast to string
else throw new IllegalArgumentException
Was This Post Helpful? 1
  • +
  • -

#3 rfs02  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • Posts: 70
  • Joined: 30-September 12

Re: Generic Casting Question

Posted 12 October 2012 - 08:00 AM

But then you would have to pass a "Object" to the method - not a great design when you know you can be more specific.

The way I would structure this is by using a private method that takes the "Scanner" object and does the actual work, and 2 overloaded methods that create the scanner method based on the argument passed in.

private ArrayList<Byte> getBytesInternal(Scanner scn) { ... }

public ArrayList<Byte> getBytes(File f) { return getBytesInternal(new Scanner(f)); }

public ArrayList<Byte> getBytes(String s) { return getBytesInternal(new Scanner(s)); }


Was This Post Helpful? 1
  • +
  • -

#4 crazedmeph  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 93
  • Joined: 19-October 09

Re: Generic Casting Question

Posted 12 October 2012 - 08:10 AM

Yea i know how to do it that way, but I was wanting to see if you could cast a object or do something like that with generics. It might not be possible but I thought it might be :P
Was This Post Helpful? 0
  • +
  • -

#5 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 714
  • Joined: 03-August 06

Re: Generic Casting Question

Posted 12 October 2012 - 08:40 AM

Only send a string, wherein the string is the file location or it's just a normal string. Then test within your method if the string is a valid file. Otherwise, assume it is just a string.
Was This Post Helpful? 0
  • +
  • -

#6 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1132
  • View blog
  • Posts: 2,490
  • Joined: 05-May 05

Re: Generic Casting Question

Posted 12 October 2012 - 09:38 AM

rfs02 gave you the most sensible way to do it. There's absolutely nothing wrong with adding short methods to transform the source into something suitable for the internal workhorse method. Casting should always be avoided it possible. Generics weren't made for what you're trying to do. They are a type-safety mechanism used to restrict what types go in and out of a method or data structure. Your problem isn't restricting types, it transforming them into suitable ones.

Quote

Only send a string, wherein the string is the file location or it's just a normal string.


Can you imagine the API for that?

This post has been edited by blackcompe: 12 October 2012 - 09:38 AM

Was This Post Helpful? 1
  • +
  • -

#7 rfs02  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • Posts: 70
  • Joined: 30-September 12

Re: Generic Casting Question

Posted 12 October 2012 - 10:01 AM

So to summarize:

  • Generics are not made for type checking - if all the different types had a shared interface or parent class, you might have been able to pass that in or use a Bound generic type
  • You can pass an Object and check the type inside the method, but whoever is calling the method will hate you forever when they start getting exceptions
  • You can overloaded methods

Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2558
  • View blog
  • Posts: 10,679
  • Joined: 20-September 08

Re: Generic Casting Question

Posted 12 October 2012 - 10:17 AM

You could always make the parameter a Reader. Pass FileReader or StringReader. The code will work in the same way for either parameter type
Was This Post Helpful? 1
  • +
  • -

#9 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1132
  • View blog
  • Posts: 2,490
  • Joined: 05-May 05

Re: Generic Casting Question

Posted 12 October 2012 - 10:20 AM

Quote

Generics are not made for type checking


Generics are made for type checking.

This post has been edited by blackcompe: 12 October 2012 - 10:20 AM

Was This Post Helpful? 0
  • +
  • -

#10 rfs02  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • Posts: 70
  • Joined: 30-September 12

Re: Generic Casting Question

Posted 12 October 2012 - 10:50 AM

View Postblackcompe, on 12 October 2012 - 10:20 AM, said:

Quote

Generics are not made for type checking


Generics are made for type checking.

Of course you're right, would have helped if I had read what I actually typed :).
Was This Post Helpful? 0
  • +
  • -

#11 crazedmeph  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 93
  • Joined: 19-October 09

Re: Generic Casting Question

Posted 12 October 2012 - 01:14 PM

Thanks for the help guys :), thanks for the information! You guys are awesome, can always rely on dreamincode for answers :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1