ModelMapper处理java 8可选字段为Optional (ModelMapper handling java 8 Optional fields to Optional)
我一直在应用程序周围使用modelmapper和java 8 Optionals,它们工作正常,因为它们是原始类型; 直到我将我的一个模型对象的字段更改为Optional类型。 然后一切都崩溃了。 事实证明,许多库无法很好地处理泛型。
这是结构
public class MyObjectDto { private Optional<MySubObjectDto> mySubObject; } public MyObject { privae Optional<MySubjObject> mySubObject; }
当我尝试将
MyObjectDto
映射到MyObject
,模型映射器调用public void setMySubObject(Optional<MySubObject> mySubObject){ this.mySubObject = mySubObject; }
with
Optional<MySubObjectDto>
,我不明白这是怎么回事(它们之间没有继承)。 当然崩溃很快。 现在我已经改变我的二传手接受Dto类型只是为了生存一天,但从长远来看这不会起作用。 有没有更好的方法解决这个问题,还是我应该创建一个问题?I've been using modelmapper and java 8 Optionals all around the application which was working fine because they were primitive types; until I changed one of my model objects' field to Optional type. Then all hell broke loose. Turns out many libraries cannot handle generics very well.
Here is the structure
public class MyObjectDto { private Optional<MySubObjectDto> mySubObject; } public MyObject { privae Optional<MySubjObject> mySubObject; }
When I attempt to map
MyObjectDto
toMyObject
, modelmapper callspublic void setMySubObject(Optional<MySubObject> mySubObject){ this.mySubObject = mySubObject; }
with
Optional<MySubObjectDto>
, which I don't understand how that's even possible (there is no inheritance between them). Of course that crashes fast. For now I've changed my setters to accept Dto type just to survive the day but that's not going to work on the long run. Is there a better way to get around this, or shall I create an issue?
原文:https://stackoverflow.com/questions/28603853
最满意答案
您应该只使用一种类型的选择器。 例如,在你的情况下:
values-sw420dp
values-sw480dp
values-sw560dp
values-sw600dp
values-sw720dp
应该足够了(但显然取决于应用程序和要完全支持的设备的要求)。 你为什么还使用基于密度的选择器(如
values-mdpi
)? 正如您可能知道的那样,dp
和sp
尺寸是密度独立的。值得一提的是,如果您仔细设计布局,可以减少需要显式声明的维数。 例如,您可以将
LinearLayout
与称重的孩子一起使用。在复杂的项目中,当我希望应用程序按照屏幕大小进行缩放时,我通常使用脚本从参考文件开始生成所有维度文件,并将所需乘数应用于每个屏幕类别。 实际代码很大程度上取决于您的特定需求,但Java中的一个示例可能是:
public class GenerateDimens { public static void main(String[] args) throws Exception { String resFolder = "app/src/main/res/"; String dimensFileName = "dimens.xml"; File source = new File(resFolder + "values-sw800dp", dimensFileName); File target = new File(resFolder + "values-sw0dp", dimensFileName); DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document document = builder.parse(source); NodeList nodeList = document.getElementsByTagName("dimen"); Pattern pattern = Pattern.compile("(\\d+).*"); for (int i = 0; i < nodeList.getLength(); i++) { Node dimen = nodeList.item(i); String textContent = dimen.getTextContent(); Matcher matcher = pattern.matcher(textContent); if (matcher.find()) { String oldValue = matcher.group(1); String newValue = String.valueOf((int) (Integer.parseInt(oldValue) / 1.33)); dimen.setTextContent(textContent.replaceFirst(oldValue, newValue)); } } TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "yes"); document.setXmlStandalone(true); transformer.transform(new DOMSource(document), new StreamResult(target)); } }
You should use only one type of selector. For example, in your case:
values-sw420dp
values-sw480dp
values-sw560dp
values-sw600dp
values-sw720dp
should be enough (but obviously that depends on the requirements of the app and the device you want to fully support). Why are you using also density-based selectors (like
values-mdpi
)? As you probably know,dp
andsp
dimensions are density independent.It's worth adding that, if you design your layout carefully, you can reduce a lot the number of dimensions you need to explicitly declare. For example, you can use
LinearLayout
s with weighed children.In complex projects, when I want the app to scale with the screen size, I usually use a script to generate all the dimens files starting from a reference one, applying the desired multiplier to every screen class. The actual code depends a lot on your specific needs, but an example in Java could be:
public class GenerateDimens { public static void main(String[] args) throws Exception { String resFolder = "app/src/main/res/"; String dimensFileName = "dimens.xml"; File source = new File(resFolder + "values-sw800dp", dimensFileName); File target = new File(resFolder + "values-sw0dp", dimensFileName); DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document document = builder.parse(source); NodeList nodeList = document.getElementsByTagName("dimen"); Pattern pattern = Pattern.compile("(\\d+).*"); for (int i = 0; i < nodeList.getLength(); i++) { Node dimen = nodeList.item(i); String textContent = dimen.getTextContent(); Matcher matcher = pattern.matcher(textContent); if (matcher.find()) { String oldValue = matcher.group(1); String newValue = String.valueOf((int) (Integer.parseInt(oldValue) / 1.33)); dimen.setTextContent(textContent.replaceFirst(oldValue, newValue)); } } TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "yes"); document.setXmlStandalone(true); transformer.transform(new DOMSource(document), new StreamResult(target)); } }
相关问答
更多-
壁纸的宽度是纵向模式下的两倍。 所以Nexus One上的960x800,Droid上的960x854,G1 / Magic / Hero上的640x480等等。 A wallpaper is twice as wide as the device held in portrait mode. So 960x800 on a Nexus One, 960x854 on a Droid, 640x480 on a G1/Magic/Hero, etc.
-
如何在android中以编程方式设置SeekBar的尺寸?(how to set dimensions of SeekBar in android programatically?)[2022-01-06]
将其添加到ViewGroup时,为其指定适当的LayoutParams。 尝试这个: viewGroup.addView(mySeekBar, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 如果你需要更复杂的东西,具体取决于布局,你可以创建一个每个布局都有一个LayoutParams内部类,你可以创建一个实例并进行修改,然后使用 LayoutParams params = new LayoutParams(...); /* do stuff ... -
从另一个充当插件的Android应用程序调用方法(Call method from another Android app which is acting as an addon)[2022-03-17]
我已经解决了这个问题,因为njzk2建议。 简而言之,主应用程序发送的广播的意图包含需要完成的任务。 插件接收器拦截调用,计算结果,完成所有工作后,它以与接收任务相同的方式将响应发送回主应用程序:创建包含结果并传播结果的意图。 更多信息:在我的情况下,我需要同步调用(发送任务和等待响应)。 我没有找到任何库自动执行,所以我使用了wait和notify。 如果其他人知道更好的方式,请分享。 I have solved this as njzk2 suggested. Briefly, the main app ... -
当我需要像这样更新数据库时,我通常会使用switch语句来执行此操作,其中案例会相互泄漏,例如: switch (oldVersion) { case 1: // update to version 2 // do _not_ break; -- fall through! case 2: // update to version 3 // again, do not break; case 3: // ...
-
您应该只使用一种类型的选择器。 例如,在你的情况下: values-sw420dp values-sw480dp values-sw560dp values-sw600dp values-sw720dp 应该足够了(但显然取决于应用程序和要完全支持的设备的要求)。 你为什么还使用基于密度的选择器(如values-mdpi )? 正如您可能知道的那样, dp和sp尺寸是密度独立的。 值得一提的是,如果您仔细设计布局,可以减少需要显式声明的维数。 例如,您可以将LinearLayout与称重的孩子一起使用。 在 ...
-
显然,在API 22中添加了更改INSTALL_NON_MARKET_APPS的功能。 您可以在2014年10月21日(API 21发布后)找到代码库中的更改: 这允许工作配置文件MDM启用未知来源,即使用户没有UI。 从未知来源安装应用程序仍会提示用户使用软件包安装程序对话框,因此MDM现在可以从非市场来源安静地安装应用程序 API 21中明确缺少此功能。 Apparently, the ability to change INSTALL_NON_MARKET_APPS was added in API ...
-
经过这两行...... bmOptions.inJustDecodeBounds = true; BitmapFactory.decodeFile(image, bmOptions); ...仅当解码有效时,bmOptions才会包含有效的outHeight和outWidth。 如果没有,它们将包含-1,如您所述。 你需要调试来验证,但我怀疑图像有时是空的或以某种方式无效。 你说当你拍摄多张照片时会发生这种情况。 垃圾收集器可能无法跟上,或者您在处理图像/位图时没有释放对图像/位图的引用? 你以什么方式触 ...
-
如果将布局参数(即layout_width和layout_height )设置为wrap_content则返回的值完全正确。 您必须在XML或Java中指定minWidth和minHeight ,但无论如何您都知道最小尺寸。 If it's layout parameters (i.e. layout_width and layout_height) are set to wrap_content then the returned value is totally correct. You have to ...
-
选项B - 因为您可以从Activity类中的任何非静态方法调用getApplicationContext() 。 事实上,Activity也是从Context派生的(继承树中的某个地方......)所以你可以这样做: Cursor c = getContentResolver().... 您不必保留对上下文的引用。 特别是不静电,可能会导致问题。 你是对的 - 因为你通常不为Activity创建自己的构造函数,所以你将构造代码放在onCreate 。 Option B - Since you can c ...
-
您可能正在寻找地图转换方法 。 特别是,您可以使用: latLngToContainerPoint :给定地理坐标,返回相对于地图容器的相应像素坐标。 containerPointToLatLng :给定相对于地图容器的像素坐标,返回相应的地理坐标(对于当前缩放级别)。 // 1) Convert LatLng into container pixel position. var originPoint = map.latLngToContainerPoint(originLatLng); // 2) Ad ...