Apache Jakarta and Beyond: A Java Programmeramp;#039;s Introduction [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Apache Jakarta and Beyond: A Java Programmeramp;#039;s Introduction [Electronic resources] - نسخه متنی

Larne Pekowsky

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید







8.3. Indexed Properties


In addition to simple properties, PropertyUtils has methods for dealing with

indexed properties. Conceptually an indexed property may be thought of as an array, although more literally the requirement is that the get method must take an integer argument, and the set method must take an integer argument along with the new value. One way to get an artist's first album is fairly obvious

PropertyUtils.getIndexedProperty(artist, "orderedAlbum", 0);

PropertyUtils offers an even more convenient means to accomplish the same task. getIndexedProperty() can take a single argument with a special syntax that specifies both the name of the property and the index. This is designated by enclosing the index in brackets, just as would be done in Java code.


PropertyUtils.getIndexedProperty(artist, "orderedAlbum[0]");

The number here must be a literal; it cannot be a reference to a variable or another property.

There are also set methods that correspond to both forms of the get method:


Album firstAlbum = new Album();
...
PropertyUtils.setIndexedProperty(artist, "orderedAlbum",
0, firstAlbum);
PropertyUtils.setIndexedProperty(artist, "orderedAlbum[0]",
firstAlbum);

There is no intrinsic notion of the size of an indexed property, although many such properties are implemented as arrays that do have a size. Therefore, the behavior of these methods when asked to get or set a value beyond the number of available elements is entirely dependent on how the bean implements the get and set methods. If those methods throw an ArrayIndexOutOfBoundsException, then so will the PropertyUtil methods.

There are tradeoffs to be made when deciding how to implement indexed properties. Often it is desirable to grow a set of elements by simply setting a new value. When an artist comes out with a second album, it is convenient to be able to write


PropertyUtils.setIndexedProperty(artist, "orderedAlbum[1]",
newAlbum);

and have this automatically expand the underlying storage as needed. This is the way the beans in Listings 8.1 and 8.2 work. Indexed properties are stored in ArrayLists. If the provided index is one greater than the current size when performing a set, the element will be added instead of modified. This behavior is not strictly in keeping with the JavaBean specification, which states that the only way to change the size of an indexed property is to call a set method that replaces the current array with a new one. Developers need to weigh for themselves the pros and cons of convenience versus strict adherence to the specifications.


/ 207