Inferring return value assignment types - ever seen this ?

Consider a class designed to hold arbitrary values defined at runtime. It is backed by a Map<String,Object> and values are stored and retrieved by a String identifier. Basic type checking is also performed.
Setting the values is straightforward: obj.set(name, value) but I have two candidates for syntax of getters.
First an example of the obvious approach, where getInteger(&hellip;) performs type checking, casting and explicitly returns an Integer:
Integer id = obj.getInteger("id") ;This is ok but I discovered a more consice syntax that works without without explicitly naming a type nor sacrificing type safety any further:
Integer id = obj.get("id") ;In this approach we use a generic method, where the return type is inferred and passed to the method. Here's how I do it:
// root method that is called by both approaches
private <K> K _get(String name, Class<K> clz) {
  Object value = map.get(name) ;
  // check for null, assignability, etc.
  if (!clz.isAssignableFrom(value.getClass())) {
      throw new RuntimeException(clz + " is not assignable from " + value.getClass()) ;
  return (K) value ;
// method 1 - the obvious way
public Integer getInteger(String name) {
  return _get(name, Integer.class) ;
// method 2
public <T> T get(String name, T...ts) {
  if (ts.length > 0) throw new RuntimeException("Illegal usage...") ;
  return (T) _get(name, ts.getClass().getComponentType()) ;
}This works becaues type T is used for both the var-arg array and the return value. Of course, it probably fails if you provide additional args - you're not supposed to. ts.getClass().getComponentType() returns the type you are assigning the return value to.
Not many languages can take this into consideration. In a way, it makes the return type part of the signature.
Advantages:
- Much smaller API
- Dynamic extension for use with a new datatype doesn't require adding a new getX() method
- Members can be treated uniformly, such a through iteration
Disadvantages:
- Involve the confusing zero-length var-args in the api that must be ignored
- Type is inferred to be Object when the return value is not assigned
At first you might think that the method 1 is more type-safe but in fact it is not. Using method 1 you have to align the method with the assignment type, but you can still exception at runtime. In method2 the datatype is simply inferred.
I'm interested in your thoughts on this teqnique. Please let me know if you have a way of concealing the var-args oddness. I think this technique could complement various patterns quite well, such as factory.
Thanks for your input.
Edited by: crackleOK on Jul 21, 2009 4:46 AM

crackleOK wrote:
JoachimSauer wrote:
The only redeeming quality is that it can be very nice to use such code.API's are to use (and often have ugly guts). Unfortunately true, but not a good reason to write ugly implementation code. If you only care about code quality up to the API, then you only care about the shiny coating of your car as well and don't mind the rust as long as you don't see it.
As I said: sometimes the tradeoff is worth it, but it should still be weighted.
Also: the ignorable varargs parameters are not needed in any way whatsoever.Please explain.From what I see you can replace this method:
public <T> T get(String name, T...ts)
// with
public <T> T get(String name)without any loss of functionality.

Similar Messages

  • Value assignment and value assignment type in the specification workbench

    Hi all,
    There is this section on the specification workbench called u201CProperty Treeu201D, this section contains all the value assignment type, I have two questions on this.
    Question 1 u2013
    What is exactly the value assignment type? The SAP help portal said it is a structure specification data and information. What exactly does it mean and its usage?
    Question 2 u2013
    May I know where is the SPRO configuration that actually causes the list of value assignment types appear on the property tree section for this particular specification type which I created.
    In another word, I create this new specification with specification type u201CREAL_SUBu201C and specification category u201CSubstanceu201C in the specification workbench transaction. Next thing I know is that the property tree section for this particular specification consists of a list of value assignment type. How does the system assigned these list of value assignment types to this specification i created? Coz I didnu2019t do anything.
    Where in SPRO and the logic link it up on this??
    Thanks.
    YY

    Hello all
    EH&S contains a number of submodules (PS, DG, IH etc.). Regarding each submodule a basic customizing is delivered by SAP. One example is the specification type. Usually you will find something like
    REAL_SUB
    LIST_SUB
    etc. in the Customizing as discussed. Further more I believe with EH&S 2.7. the specification category come in place (SUBSTANCE as an example). You need to take care regarding this too.
    An value assignment type can be grouped in a property tree and a property tree can be linked to a specification type and there are cases known that one specification type is linked only to one property tree (this is a decision regarding business needs and not an IT quesition). Using the EHS& surface you can change the property tree per specification if the set up is done in Customizing. Therefore the property tree is a "view" on the data linked to a specification. (identifeirs etc. are managed different)
    A "property" is based on a value assigment type and in many cases on a related EH&S class and characteristic. You will find these types.
    A => value assignment type to be used if you need characteristics
    B => value assignment type to be used if you need specification listing
    C  => value assignment type to be used if you need composition
    .. etc.
    You can mix types. That means you can create a property of type "A" and "C" (take a look in SAP original tree; you will find examples how to do it).
    A propetry tree is a "list" of assignment type related to a object (specification). Using the property tree you can maintain necessary data. Therefore ithe property tree itself contains per specification the list of "potential" data structures you can fill with data. I belive SAP Standard is delviering now something like 250 properties (valeu assignment types): To e.g. get an MSDS you need to maintain I would assume at least something like 20 up to 50 properties
    The area of customizing regrading property tree, specification types etc.  is the most crticial to have a sucessful EH&S project running; any wrong decision gives rise to a high cost later Therefore take your time to understand what effect does which customizing acitivity have later.
    Based on the value assignemnt you will create later e.g. a WWI report (like MSDS).
    Additionally if necessary you can create customer specific value assignment types by either do a copy from SAP original to "Z" (highly recommended to do so!) or starting from basic. 
    Starting with EH&S 2.7. you have now more options to "change" EH&S using standard EH&S functionality. Take a look in customizinjg (example: you could create you own "look and feel" of the workbench (you should'nt really do that but it is possible). You can design your own "tab strips" etc.
    So once again: the "correct" set up of EH&S in the area of e.g. identifers, speficiation types, property trees etc. is the "core" activity in EH&S customizing and therefore think "twice" before you change the setup up (you need to understand what a change mean)
    With best regards
    C.B.

  • Value Assignment Type Setting

    Dear Experts,
    While creating the Value Assignment Type in SPRO
    there are following check box... which are
    CharCopy Active - Automatic Activation of Copied Value Assignments
    New Val. actv - Automatic Activation of New Value Assignments
    Change Active - Automatic Activation of Changed Value Assignments
    I have read the help for each check box but still concept is NOT so clear...
    Could you please elaborate me the significan of each check box...
    also, couuld you tell me that As per Standerd / Regular Industry Practice, which of the above check boxes should be active and which should not
    Please Guide..
    Amol

    Hello Amol
    in some sense there is no "standard" how a company should/can use these check boxes. Both options have pros/cons.
    In my opinion best practise is to "set them" (all three) and to prepare a user manual in which this "behaviour" of the system is explained.
    From the point of view of "clicks" the number of clicks are nearly the same.
    E.g.
    if you not activate them you have to set the indicator and you must save the data records and
    if you have activated and you would like to take some time to think once again about content of the data record you would remove the active indicator and save (later you must set them again to activate them and save them regarding MSDS etc.).
    In any case: to my experience the end user training regarding how to use the usage and what does the active indicator means (as well as the relevancy indicator) is not an easy task.
    By the way: there is to my knowledge no corresponding customizing activity in which you can handle how the "relevancy indicator" can be set or not. So in any case (because the existance of an activated relevancy indicator is "driving" change marks in WWI reports) end user need their "time" to understand how to use properly this usage.
    Further on they should be familiar in how to use the output variants (because using them you can get an fast overview about there a active indicator is missing etc.)
    With best regards
    C.B.
    PS: additionally: the usage on the level of the identifier is "special" in some sense. Keep in mind this (especially in WWI reports). The reason is that the usage on the identifier has been created with EH&S 2.7. So in principle: you need not to use the usage to print a identifier in a WWI report but you should do that (because by using the usage you can additionally control the access rights of users).
    PPS: one "strong" pro in my opinion to use the "active" option is this:
    If you enter e.g. a source or a user defined text in the data record this information is normally not used in a WWI report. Therefore if you haven't "activated" this in customizing the user must ! set the acitve indicator once again. This means: Any ! change in data record (together with save) will remove the active indicator by standard (without using the three check boxes).
    Edited by: Christoph Bergemann on Mar 21, 2010 8:02 AM
    Edited by: Christoph Bergemann on Mar 21, 2010 8:05 AM
    Edited by: Christoph Bergemann on Mar 21, 2010 8:06 AM
    Edited by: Christoph Bergemann on Mar 21, 2010 8:06 AM

  • EHS - value assignment types

    Hi Friends,
    I'm not an EHS expert however I've received an EHS specific query, please help me to understand and how to proceed on below requirement:
    In EHS module, create a new branch "regulatory information CLP" with 5 new properties and update old one to DSD/DPD.
    For these changes, adds and changes will have to be done in customizing in "value assignment types" entry with different valorization type (branch and properties).
    I can trace Value Assignments under property tree in SPRO but don't know how to go about.
    Regards,
    Yati

    Hello Yati,
    If I have understood correctly, you want to create / Edit a property tree. If so, thre are certain steps involved and it will not be possible to detailed it out here, but I will list out the requirements and steps involved;
    Master data required:
    1.Create characteristics for possible values to assign to the Value assignment type
    2.Create class for the value assignment type and assign characteristics to the class
    3.Create Phrase set
    4.Create phrases and assign to the phrase set
    Note: Step 3 &4 are required for phrase enable characteristics.
    Perform following Customizing settings:
    1.Basic Data and Tools -> Specification Management -> Specification Database Structure -> Settings for Value Assignment -> Specify Value Assignment Types
    2.Basic Data and Tools -> Specification Management -> Specification Database Structure -> Settings for Value Assignment -> Set Up Table-Based Value Assignment
    3.Basic Data and Tools -> Specification Management -> Specification Database Structure -> Settings for Value Assignment -> Set Up Property Tree
    Request you to visit SAP help at : http://help.sap.com/saphelp_erp60_sp/helpdata/en/c1/eda0f591ec12408b25e7a1b369ca45/frameset.htm
    and visit topic : Value Assignment  under Basic Data and tools -> Specification Editing
    Hope this information is helpful to you.
    Regards,
    Niraj

  • TS1717 I get the following error message after installing the latest version of itunes via auto update:  "the itunes Library file cannot be saved. An unknown error occurred (-54). Has anyone ever seen this message and anyone have a suggestion what to do?

    I get the following error message after installing the latest version of itunes via auto update:  "the itunes Library file cannot be saved. An unknown error occurred (-54). Has anyone ever seen this message and anyone have a suggestion what to do?

    Hi pholewinski!
    I have a couple of articles for you here that I believe will help you with this issue. The first is an article on advanced troubleshooting, and it can be found here:
    iTunes: Advanced iTunes Store troubleshooting
    http://support.apple.com/kb/ts3297
    If following that article's steps doesn't take care of the issue, you can try the things listed in this article, which is about permissions issues in iTunes:
    iTunes: Missing folder or incorrect permissions may prevent authorization
    http://support.apple.com/kb/TS1277
    Thanks for using the Apple Support Communities. Have a good one!
    -Braden

  • Has anyone ever seen this error

    We just launched a vaccation tracking app with a coldfusion
    front end and an access 97 backend (dont ask). The app was tested
    for over a month and passed all tests. However we never tested for
    volume, or amount of users hitting the app. On the backend, we have
    an access table linked to a text file. Again, this tested out
    perfectly over the past month. This morning after release to the
    entire company of about 2,000 people, users began reporting this
    error:
    The text file specification 'EmployeeList Link
    Specification6' does not exist. You cannot import, export, or link
    using the specification.
    Some users logged in to the app successfully and some
    recieved this error. For those that recieved the error, we had them
    back out and re-login and then the app worked just fine.
    Has anyone ever seen this or have a guess as to what is going
    on?

    Hi,
    The right way would be to use a "real" database server.
    But you can test if db-caching can improve the issue.
    With each <cfquery> you can set a cachedwithin
    parameter. Using that should allow you to query the Access db once,
    and then have CF keep the query results in memory (for the DB as
    well as the text-file).
    You could query the complete database with no filtering this
    way, and then filter the data using QueryofQuery to output only
    records for specific users/groups/regions.
    You also need to make sure the settings in CF admin are
    configured right for this.
    The more often the database content is updated, the slower
    this solution will get though.
    cheers,
    fober
    ps. for the cachedwithin parameter in the query set a
    timeframe of forever, so the recordset never gets updated, and then
    actually execute a new query to update the recordset at the end of
    your "save a vacation" or "updated a vacation" functions. This way
    the app would only read the db when a real change to the records
    occurred.
    But like I said with 2000 users, even that may not be enough
    to get the app performing.

  • Anyone Ever Seen This Before?

    Hello,
    Has anyone ever seen this before: http://digitalmarketing1.com/MacErrorPicture.jpg
    Running an IMac Intel Core 2 Duo 2.33 GHz, Microsoft Entourage 2004 for Mac v 11.3.6.
    Opened Entourage and selected some of the tons of spam > hit Delete and there you have it! Wasn't really sure how to answer this one! It was like a Bill Gates trick question for Mac users. I had to have someone else come in the office to make sure I wasn't having a stroke or something.
    I did eventually select the highlighted button after a coin flip, but to no avail. Nothing happened. Selected the other button then thinking that MUST be the right answer! Nothing...
    Finally, an end task was the right answer. Seems to have gone away for now, but Entourage still crashes quite often. Rebuilt email, repaired permissions, etc. Nothing appears to be wrong, but clearly something was amiss.
    Any ideas?

    An unexpected exception has been detected in native
    code outside the VM.
    Unexpected Signal : EXCEPTION_ACCESS_VIOLATION
    occurred at PC=0x92150b9
    Function name=(N/A)
    I get this error when I call the same native function
    twice.Yes, many people have seen it many times when doing JNI.
    The usual reasons;
    -In the vast majority of cases (think 99.9%) there is a bug in the C/C++ code that does JNI. As usual a pointer is the problem.
    -Very, very rarely there might be a problem with something else. Like the jvm or a third party library.

  • Has anyone ever seen this?

    Ive been having major problems with my ipod
    -making a scratching sound when trying to load
    -Folder with exclamation point is showing
    -cant get to play any music whatsoever.
    So I been on here for about 5 hours trying to figure it out
    I tried reseting on the computer. that worked until I tried to reload my songs on the ipod.
    So I got rid of all ipod updaters on my computer and reinstalled the newest one.
    and now this happened
    http://img72.imageshack.us/img72/6141/ipodservice9hb.png
    Has anyone ever seen this before?
    Can anyone help me out?
    thanks
    sorry if this is the wrong forum

    Well Tyler....I've seen something like your problem before, but i'm afraid i can't be of much help. I saw the problem because it happened to me before, and my iPod needed to be sent into Apple for repair, i believe they need to reconnect the hard drive on it....well hopefully someone here can help you out, because mine was a matter of being sent in.

  • Assessment in the value assignment type

    Dear all,
    May I know what is the usage on the following assessment in the value assignment type section:
    0 Not Assessed
    1 Valid w/o restriction
    2 Valid with restriction
    3 Invalid
    4 Cannot Asseed.
    Why we need those assessment for each of the VA instance, what is the usage?
    Thanks
    YY

    Hi,
    Assessment tab is used to comment on the quality of the data the VA holds, you can say it as "not assessed" if its not checked by anyone,cannot be assessed likewise use other options.

  • Sorting of value assignment types

    Hi
    Is there any way out for sorting already created custom value assignment types. As there is requirement to rearrange the tabs in Incident screen (CBIH82)
    Currently value assignment types have been sorted alphabetically, and if we change the name of class and value assignment types for rearrangement, it may have larger impact because of its current assignments/usage.
    Is there any other mechanism to sort them. 
    Regards
    Vikram

    Thanks Neeraj
    But i am bit worried for changing the name of VAT, as existing VAT has been used in various custom programs. It may hamper/impact those programs.
    I may have to look for some other work around or i have to do some impact analysis to find all the custom programs which can be affected due to change in name.
    Regards and Thanks
    Vikram

  • Hi I am receiving thousands, yes thousands of eMails from APPLE Support Communities, In My inBox there is more than 26.000 eMails from these communities, and i Need Help from you, i will appreciate.I am not panicking but i've ever seen this !

    Apple Support Communities
    HI
    I am receiving THOUSANDS, YES THOUSANDS of eMails from ALL Apple Support Communities.I deleted some one thousand but still more than Normal.
    Please i need your Help.This is Enormous!!! I've never seen this happening! It should be a huge mistake that everybody from All Apple Support Communities
    sending me their complains? I think it is a big Mistake, or Something bigger, i don't Know! I just need Help in this moment. it's been more than three or four days
    i saw a number of eMails from Apple Support communities landing in my eMail address, should this be normal? I don't think so.
    I appreciate any Help
    Kind Regards
    & a Happy New Year

    Click Your Stuff in the upper right and select Profile. In your profile there is a link to the right to manage email notifications.

  • Restart Needed Warning...anyone ever seen this before?

    I've owned my MacBook for nearly 5 years now, and this is the very first time I've ever received this notification, so needless to say I'm a bit skeptical of it. Please let me know if this is "normal" or whether I should be suspicious of it.
    PS--Sorry for the very poor quality of the picture.

    About "You need to restart your computer" (kernel panic) messages
    Kernel Panics on Startup
    OS X- About kernel panics
    OS X- How to log a kernel panic

  • My iphone is in permanent super zoom mode; can't get to apps or anything; has anyone ever seen this?

    my iphone is in permanent super zoom mode. I can't get to apps, phone,or anything. Have tried powering down, resyncing, and i have the latest software update.  Have never seen this before.

    After you do what wjosten said, to disable zoom:
    Settings > General > Accessibility > Zoom > OFF

  • Ever seen this error message? You tried to use HTML or other restricted code. We, currently, do not allow HTML or other types of code in our message board posts or signatures. Try again with simple text only.

    Trying to load this site
    http://southcarolina.247sports.com/
    and it redirects me to this address
    http://media.247sports.com/Oops.html?aspxerrorpath=/
    and gives me the error message. "You tried to use HTML or other restricted code. We, currently, do not allow HTML or other types of code in our message board posts or signatures. Try again with simple text only. "
    Thanks for your help

    Clear the cache and the cookies from sites that cause problems.
    * "Clear the Cache": Tools > Options > Advanced > Network > Offline Storage (Cache): "Clear Now"
    * "Remove the Cookies" from sites causing problems: Tools > Options > Privacy > Cookies: "Show Cookies"
    Start Firefox in [[Safe Mode]] to check if one of the add-ons is causing the problem (switch to the DEFAULT theme: Tools > Add-ons > Themes).
    * Don't make any changes on the Safe mode start window.
    See:
    * [[Troubleshooting extensions and themes]]

  • Change in the behaviour of the value assignment type after the ECC upgrade

    Hello Team,
    We have recently upgraded our system to ECC 6.
    In the older version of SAP whenever we try to open a phrase related property (in a particular substance specification) it used to give a warning message upfront in case any of the phrases used is not assigned to the phrase set.
    But with the upgraded version this upfront warning message doesnot appear and we have to click on individual phrases to check if that phrase has any issue or not. This is quite time consuming in the case several phrases has been assigned to a property.
    Please let me know if we have any solution for this available.
    Regards,
    Anoop

    Hello Anoop
    during our upgrade project we did not have the same problem. I have never heard about a similar problem. May be check OSS (SAP marketplace). Depending on the release you started may be you missed yome customizing to be done (if you started with SAP enterprise 4.7 Ext 1.1 the change is "dramatic"; if you started with SAP enterprise 4.7 Ext 2.0 changes should only be minor; in case of ECC 5.0: I believe there is no change.
    a.) Do you have activated Enhancement Packages?
    b.) do you use the "exit" to prepare "dynamic phrase sets"?
    With best regards
    C.B.

Maybe you are looking for