public void di1() { UI myUI = null; // tag::di1[] Optional<DeviceInfo> deviceInfo = DeviceInfo.get(myUI); // <1> deviceInfo = DeviceInfo.get(); // <2> DeviceInfo info = DeviceInfo.require(); // <3> info.getAcceptHeader(); // <4> info.getUserAgentHeader(); // <5> info.getWindowWidth(); // <6> info.getWindowHeight(); // <7> info.isSmartphone(); // <8> // ... // end::di1[] }
/** * Get the {@link DeviceInfo} for the current UI, throwing a {@link IllegalStateException} if not available. * @return The {@link DeviceInfo} for the current UI * @throws IllegalStateException If a DeviceInfo for current UI is not available */ static DeviceInfo require() { return get().orElseThrow(() -> new IllegalStateException("A DeviceInfo for current UI is not available")); }
/** * Get the {@link DeviceInfo} for the current UI, if available. * @return Optional {@link DeviceInfo} for the current UI */ static Optional<DeviceInfo> get() { final UI ui = UI.getCurrent(); if (ui != null) { return get(ui); } return Optional.empty(); }